Поэтому я только начал разработку Android, и я уже ударил стену. Я просматривал много онлайн и на этом сайте, но не могу найти ответ, который работает для меня. Все, что я записал до сих пор в моем файле Activity, следующее.
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>
Просто добавьте следующую строку к вашей android:id="@+id/my_button"
то в рамках вашей деятельности вы можете использовать метод findViewById(R.id.my_button);
Надеюсь это поможет. Я определенно рекомендую вам прочитать это → http://developer.android.com/guide/topics/ui/declaring-layout.html
Вы не указали идентификатор в своем XML файле. Пожалуйста, укажите идентификатор в вашем xml файле, а затем ваша ошибка будет решена.
благодаря
id
. Отсюда и ошибка.android:onClick
, зачем вам нужно найти определенную кнопку?