アラームを処理中で、2つのボタン(更新と削除)で各コンテナにアラームのリストを表示したいとします。 ListViewの2つのボタンなしでうまく動作しますが、私は二つのボタンを追加するときcurrent.classが負荷にしようとしたとき、私はこのエラーを取得する:ListViewのNullエラーボタン
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
私はちょうどのような単純なものまでをいじりだと確信しています私はいつもそうしており、私がそれを理解するのに役立つ例は見つけられませんでした。 RecyclerViewはlistviewの高度なバージョンであり、ボタンでうまくいきますが、後者のことを学びたいので、現在のコードを動作させてデバッグすることができます。以下は、リストビュー用の.javaと.xmlです。たとえ答えがある別のサイトへのポインタであっても、どんな助けも素晴らしいだろう。可能であれば、私が学ぶことができるように、私が間違ったことを得て説明したいと思います。
current.java
public class current extends ListActivity {
TextView alarm_Id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.current_alarm_list);
final AlarmRepo repo = new AlarmRepo(this);
//get string of alarm name and id
ArrayList<HashMap<String, String>> AlarmList = repo.getAlarmList();
if(AlarmList.size()!=0) {
ListView lv = getListView();
Button update = (Button) lv.findViewById(R.id.btnUpdate);
update.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
alarm_Id = (TextView) v.findViewById(R.id.my_alarm_Id);
String alarmId = alarm_Id.getText().toString();
Intent objIndent = new Intent(getApplicationContext(),alarmUpdate.class);
objIndent.putExtra("alarm_Id", Integer.parseInt(alarmId));
startActivity(objIndent);
}
});
Button delete = (Button) lv.findViewById(R.id.btnDelete);
delete.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
alarm_Id = (TextView) v.findViewById(R.id.my_alarm_Id);
String alarmId = alarm_Id.getText().toString();
int idalarm = Integer.parseInt(alarmId);
repo.delete(idalarm);
startActivity(new Intent(current.this, current.class));
finish();
}
});
String[] from = { "id", "Alarm_Name" };
int[] to = {R.id.my_alarm_Id, R.id.my_alarm_name};
SimpleAdapter adapter = new SimpleAdapter(current.this, AlarmList, R.layout.view_alarm_entry, from, to);
setListAdapter(adapter);
}else{
Toast.makeText(this,"No alarms!",Toast.LENGTH_LONG).show();
}
}
current_alarm_list.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.electrohaven.i_hate_ms.counterclockwise.MainActivity">
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
>
</ListView>
view_alarm_entry.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/my_alarm_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingStart="6dip"
android:paddingTop="6dip"
android:textSize="22sp"
android:textStyle="bold"
android:layout_toStartOf="@+id/btnUpdate"/>
<TextView
android:id="@+id/my_alarm_Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/my_alarm_name"
android:layout_toStartOf="@+id/btnUpdate"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/my_alarm_name"
android:layout_alignBottom="@+id/my_alarm_name"
android:layout_alignParentEnd="true"
android:id="@+id/btnUpdate"
android:text="Update"
android:focusable="false"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnDelete"
android:layout_below="@+id/my_alarm_Id"
android:layout_alignParentEnd="true"
android:text="delete"
android:focusable="false"
/>
ありがとうございます – MrWolf