问题
The Exception
:
Unable to start activity ComponentInfo{com.scytec.datamobile.vd.gui.android/com.scytec.datamobile.vd.gui.android.SelectedList}: java.lang.NullPointerException..
I just want to show checkbox list view and on every check it display "checked", simply but i don't know why this gives me an exception.
public class SelectedList extends Activity implements IObserver{
private ListView machine_listview;
ArrayAdapter<String> adapter;
ArrayList<String> arrayListofMachines;
ArrayList<String> arrayListofMachineNumbers;
Vector<MDCMachineStatus> machineStatus_vector;
Handler handler;
private static int oldPosition = 0;
private Boolean firstClick = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.machinelistview);
machine_listview = (ListView) findViewById(R.id.machine_listview);
machine_listview.setFastScrollEnabled(true);
MachineStatusSingleton.Register(this);
getData();
adapter = new ArrayAdapter<String>(SelectedList.this, R.layout.selectedlist,R.id.text1, arrayListofMachines);
machine_listview.setAdapter(adapter);
machine_listview.setSelection(oldPosition);
CheckBox chk=(CheckBox)findViewById(R.id.check);
chk.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
TextView txt=(TextView)findViewById(R.id.xtra);
if (arg1)
Log.d("", "abul, checked") ;
else
Log.d("", "abul, not checked") ;
}
}
);
machine_listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
oldPosition = position;
MachineStatusSingleton.setMachineNumber(arrayListofMachineNumbers.get(position));
SelectedList.this.finish();
}
});
handler = new Handler(){
public void handleMessage(android.os.Message msg) {
machine_listview.setAdapter(adapter);
adapter.notifyDataSetChanged();
};
};
}
public void Update(ISubject arg0) {
// TODO Auto-generated method stub
}
@Override
public void onDestroy()
{
super.onDestroy();
MachineStatusSingleton.Unregister(this);
}
private void getData(){
machineStatus_vector = MachineStatusSingleton.GetData();
arrayListofMachines = new ArrayList<String>();
arrayListofMachineNumbers = new ArrayList<String>();
for(MDCMachineStatus temp: machineStatus_vector){
arrayListofMachines.add(temp.toString());
arrayListofMachineNumbers.add(temp.getNumber());
}
Collections.sort(arrayListofMachines);
Collections.sort(arrayListofMachineNumbers);
}
private void updateData(){
getData();
handler.sendEmptyMessage(0);
adapter.notifyDataSetChanged();
int index = machine_listview.getFirstVisiblePosition();
View v = machine_listview.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
// ...
// restore
machine_listview.setSelectionFromTop(index, top);
}
}
回答1:
We run our app very well and suddenly we encounter NullPointerException or Unable to start activity etc errors.
Basically NullPointerException or Unable to start activity occurs when there is issue in onCreate() method of our Activity.
This occurs when :
We change any xml values of layout related to this Activity
If we do not map xml UI's properly in our Acivity
Try to access UI which is in another layout file.
Solution :
First Cross check all the mapped elements
Give unique naming
回答2:
Directly after:
TextView txt=(TextView)findViewById(R.id.xtra);
... add this:
if (txt == null) { Log.w("", "TextView is null"); }
Assuming your null pointer exception doesn't occur until you select the checkbox, that sounds like the most likely issue. I've encountered the same when I forget that I removed the corresponding element from the XML layout, or if I got the ID wrong. Usually I wrap any actions upon an element returned by "findViewById" within a null check, to ensure that even if the retrieval fails, the app at least won't crash.
回答3:
Looks like you're assigning to chk
, and subsequently, txt
by calling findViewById
as you declare them. I had to declare them first, and then assign to them using findViewById
.
来源:https://stackoverflow.com/questions/9208023/unable-to-start-activity-componentinfo-java-lang-nullpointerexception