1

Android開発を始めたばかりで、すでに壁にぶつかっています。私はオンラインやこのウェブサイトで多くのことを見回してきましたが、私に合った答えが見つからないようです. これまでにアクティビティ ファイルに書き留めたのは、次の内容だけです。

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class CommanderActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
  {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     final Button button = (Button) findViewById( R.id.button_id );
  }    
}

エラーが発生するのは、コードの最後の行 ( R.id.button_id ) です。私の XML ファイルは次のようになります。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <TableRow>
    <Button 
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="@string/Parameters"
        android:onClick="openParameters" />

    <Button
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="@string/Waves"
        android:onClick="openWaves" />
    </TableRow>
    <TableRow>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/MoreParameters"
        android:onClick="openMoreParameters" />
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/Status"
        android:onClick="openStatus" />
    </TableRow>
    <TableRow>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/Operations"
        android:onClick="openOperations" />

    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/Monitor"
        android:onClick="openMonitor" />
    </TableRow>
    <TableRow>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/CEBus"
        android:onClick="openCEBus" />
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="@string/Advanced"
        android:onClick="openAdvanced" />
    </TableRow>
</TableLayout>
</LinearLayout>
4

2 に答える 2

3

次の行をボタンに追加するだけandroid:id="@+id/my_button"
で、アクティビティ内でメソッドを使用できますfindViewById(R.id.my_button);

お役に立てれば。これを読むことを強くお勧めします-> http://developer.android.com/guide/topics/ui/declaring-layout.html

于 2012-06-04T18:15:07.183 に答える
2

xml ファイルに ID が指定されていません。xml ファイルにも ID を入力してください。そうすれば、エラーが解決されます。

ありがとう

于 2012-06-04T18:06:17.953 に答える