2016-12-06 68 views
0

我正在處理一個鬧鐘,我想用兩個按鈕(更新和刪除)向每個容器顯示一個鬧鐘列表。 ListView的作品就好,而不兩個按鈕,但是當我在添加兩個按鈕,當current.class嘗試加載我得到這個錯誤:ListView上的按鈕上的空錯誤

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" 
    /> 

回答

0

這兩個按鈕在那時不存在,它們屬於一個列表項,而不是ListView。

我的解決辦法,但它只是一個骨架,你必須用你的代碼填充它:

創建AlarmAdapter類:

public class AlarmAdapter extends BaseAdapter { 
TextView alarm_Id; 

private final ArrayList<Map<String, String>> alarmList; 

public AlarmAdapter(final Context context, ArrayList<Map<String, String>> alarmList, String[]) { 
    this.alarmList = alarmList; 
} 

public int getCount() { 
    return alarmList.size(); 
} 

@Override 
public Object getItem(int i) { 
    return alarmList.get(i); 
} 

@Override 
public long getItemId(int i) { 
    return i; 
} 

public View getView(final int position, View convertView, final ViewGroup parent) { 

    LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(
      Context.LAYOUT_INFLATER_SERVICE); 
    final View itemView = inflater.inflate(R.layout.view_alarm_entry, null); 

    View v; 

    Button update = (Button) itemView.findViewById(R.id.btnUpdate); 
     update.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View v){ 
       //onClick code 
       Toast.makeText(parent.getContext(), "Update: " + alarmList.get(position).toString() ,Toast.LENGTH_LONG).show(); 
      } 
     }); 

    Button delete = (Button) itemView.findViewById(R.id.btnDelete); 
    delete.setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View v){ 
      //onClick code 
      Toast.makeText(parent.getContext(), "Delete: " + alarmList.get(position).toString() ,Toast.LENGTH_LONG).show(); 
     } 
    }); 

    return itemView; 
} 

在活動將這個給的onCreate功能:

if(AlarmList.size()!=0) { 
     String[] from = { "id", "Alarm_Name" }; 
     int[] to = {R.id.my_alarm_Id, R.id.my_alarm_name}; 

     AlarmAdapter adapter = new AlarmAdapter(getApplicationContext(), AlarmList); 
     setListAdapter(adapter); 

    }else{ 
     Toast.makeText(this,"No alarms!", Toast.LENGTH_LONG).show(); 
    } 
+0

謝謝你的工作 – MrWolf