我一直收到这个错误,我不确定导致它的原因。我声明并初始化了变量,但错误并没有消失。我一直在看同样的错误,但我无法抓住这个错误。
05-01 20:57:43.162 32675-32675/com.ammar.customlistview1.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40be21f8)
05-01 20:57:43.172 32675-32675/com.ammar.customlistview1.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ammar.customlistview1.app/com.ammar.customlistview1.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1810)
at com.ammar.customlistview1.app.MainActivity.<init>(MainActivity.java:16)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1319)
at android.app.Instrumentation.newActivity(Instrumentation.java:1026)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4511)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
at dalvik.system.NativeStart.main(Native Method)
这是我的班级代码:
package com.ammar.customlistview1.app;
/**
* Created by Ammar on 5/1/2014.
*/
public class person {
private String name = "Ammar";
private int age = 21;
private int picture = R.drawable.ic_launcher;
public person(String name, int age, int picture) {
this.name = name;
this.age = age;
this.picture = picture;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getPicture() {
return picture;
}
public void setPicture(int picture) {
this.picture = picture;
}
}
这是我的主要活动代码:
package com.ammar.customlistview1.app;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends Activity {
personadaptor adapter ;
ArrayList <person> theperson = new ArrayList<person>();
ListView lv = (ListView) findViewById(R.id.lvmain);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
person p1 = new person("Ammar", 21 , R.drawable.ic_launcher);
person p2 = new person("Ali", 25 , R.drawable.ic_launcher);
person p3 = new person("Saber", 23 , R.drawable.ic_launcher);
theperson.add(p1);
theperson.add(p2);
theperson.add(p3);
adapter = new personadaptor(theperson, this);
lv.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:5)
在您拨打findViewById()
之前,您无法致电setContentView()
。
答案 1 :(得分:0)
您不能在Activity(或类似)类的初始值设定项中使用Android UI方法,例如findViewById(),因为这些方法仅在调用onCreate()期间和之后有效。
将初始化代码移至onCreate()
(当你这样做时,一定要把setContentView()之后的findViewById()放到Matiash中,这样才能正确指出 - 否则你只会得到一个不同的空指针异常)
答案 2 :(得分:0)
1.ListView lv; Listview初始化应该在
之后setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lvmain);
2.无需亲自初始化此名称,年龄和图片
private String name;
private int age ;
private int picture ;
3.in adapterclass你需要布局
adapter = new personadaptor(theperson, this);//where is your row layout