所以我正在爲學生製作一個非常簡單的類組織者,並且我在AddCreate中爲Add類活動獲取了nullPointerException。我真的不知道我爲什麼要這樣做。任何幫助,將不勝感激!Android中onCreate的NullPointerException
下面是在MainActivity:
public class MainActivity extends Activity {
ListView classList;
Button addClass;
ArrayAdapter<Class> adapterClass;
ArrayList<Class> currClasses = new ArrayList<Class>();
ClassesSingleton myClasses;
int REQUEST_CODE_ADD = 318;
int REQUEST_CODE_EDIT = 319;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
classList = (ListView) findViewById(R.id.classList);
final Context context = this;
adapterClass = new ArrayAdapter<Class>(this, R.layout.single_list_item, R.id.label, currClasses);
classList.setAdapter(adapterClass);
addClass = (Button) this.findViewById(R.id.addClass);
classList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView <?> arg0, View view, int position, long id){
Intent intent = new Intent(MainActivity.this, ShowClass.class);
startActivityForResult(intent, REQUEST_CODE_EDIT);
}
});
addClass.setOnClickListener(new OnClickListener() {
public void onClick(View arg0){
Intent intent = new Intent(MainActivity.this, AddClass.class);
startActivityForResult(intent, REQUEST_CODE_ADD);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == REQUEST_CODE_ADD){
Class newClass = new Class();
if(data.hasExtra("className")){
newClass.setName(data.getExtras().getString("name"));
}
if(data.hasExtra("number")){
newClass.setNumber(data.getExtras().getInt("number"));
}
if(data.hasExtra("students")){
ArrayList<Student> s = data.getExtras().getParcelableArrayList("students");
newClass.setStudents(s);
}
myClasses = ClassesSingleton.getInstance();
updateClassList(newClass);
myClasses.setClassArray(currClasses);
}
else if(resultCode == RESULT_OK && requestCode == REQUEST_CODE_EDIT){
}
}
public void updateClassList(Class n){
adapterClass = null;
currClasses.add(n);
adapterClass = new ArrayAdapter<Class>(this, R.layout.single_list_item, R.id.label, currClasses);
classList.setAdapter(adapterClass);
adapterClass.notifyDataSetChanged();
}
}
這裏的第二活動:
public class AddClass extends Activity{
Button addStudent, saveClass, cancelClass;
EditText className, classNumber;
ListView studentList;
ArrayAdapter<Student> adapterStudent;
ArrayList<Student> currStudents = new ArrayList<Student>();
int REQUEST_CODE_ADDSTU = 317;
int REQUEST_CODE_EDSTU = 316;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setContentView(R.layout.new_class);
addStudent = (Button) findViewById(R.id.addStudent);
saveClass = (Button) findViewById(R.id.confirmClassAdd);
className = (EditText) findViewById(R.id.newClassName);
classNumber = (EditText) findViewById(R.id.newClassNumber);
studentList = (ListView) findViewById(R.id.newStudentList);
adapterStudent = new ArrayAdapter<Student>(this, R.layout.single_list_item, R.id.label, currStudents);
studentList.setAdapter(adapterStudent);
studentList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView <?> arg0, View view, int position, long id){
Intent intent2 = new Intent(AddClass.this, EditStudent.class);
startActivityForResult(intent2, REQUEST_CODE_EDSTU);
}
});
addStudent.setOnClickListener(new OnClickListener() {
public void onClick(View arg0){
Intent intent2 = new Intent(AddClass.this, AddStudent.class);
startActivityForResult(intent2, REQUEST_CODE_ADDSTU);
}
});
saveClass.setOnClickListener(new OnClickListener() {
public void onClick(View arg0){
Intent data = new Intent();
int numData = 0;
if((className.getText() != null) && (classNumber.getText() != null) && (currStudents.isEmpty() != true)){
String nameData = className.getText().toString();
String numStr = classNumber.getText().toString();
ArrayList students = currStudents;
boolean intTrue = true;
try{
numData = Integer.parseInt(numStr);
}catch(NumberFormatException e){
Toast.makeText(AddClass.this, "Please be sure to enter a numberic value in the number field.", Toast.LENGTH_LONG).show();
intTrue = false;
}
if(intTrue){
data.putExtra("className", nameData);
data.putExtra("number", numData);
data.putParcelableArrayListExtra("students", students);
setResult(RESULT_OK, data);
finish();
}
}
else{
Toast.makeText(AddClass.this, "Don't leave your class name or number field blank!", Toast.LENGTH_LONG).show();
}
}
});
}
//onActivityResult callback
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == REQUEST_CODE_ADDSTU){
Student newStudent = new Student();
if(data.hasExtra("name")){
newStudent.setName(data.getExtras().getString("name"));
}
if(data.hasExtra("id")){
newStudent.setId(data.getExtras().getInt("id"));
}
updateStudentList(newStudent);
}
}
public void updateStudentList(Student s){
adapterStudent = null;
currStudents.add(s);
adapterStudent = new ArrayAdapter<Student>(this, R.layout.single_list_item, R.id.label, currStudents);
studentList.setAdapter(adapterStudent);
adapterStudent.notifyDataSetChanged();
}
}
這裏的堆棧跟蹤:
FATAL EXCEPTION: main
Process: com.example.congelassign2, PID: 1654
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.congelassign2/com.example.congelassign2.AddClass}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.congelassign2.AddClass.onCreate(AddClass.java:55)
at android.app.Activity.performCreate(Activity.java:5243)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
... 11 more
編輯:我添加了參考saveClass按鈕(這是一個愚蠢的錯誤,感謝球員),但我仍然在同一條線上得到同樣的錯誤...
哪家線AddClass 55? –
保存類的onclicklistener – user3349697
你忘了實例化那個''Button'就像你用其他'Views' – codeMagic