您好我正在使用android.How我可以在Onpostexecution的异步任务中使用setContentview吗?我用的是代码
if (getIntent().hasExtra("key")) {
if(key.compareTo(App_id)==0)
{
setContentView(R.layout.home);
}
}
else {
finish();
}
但它强制关闭并显示错误nullpointer异常。如何在onpost execute ??
中使用setcontentview这是我的logcat输出
03-20 13:47:30.270: E/AndroidRuntime(1628): FATAL EXCEPTION: main
03-20 13:47:30.270: E/AndroidRuntime(1628): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.meetap.AdHome}: java.lang.NullPointerException
03-20 13:47:30.270: E/AndroidRuntime(1628): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
03-20 13:47:30.270: E/AndroidRuntime(1628): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-20 13:47:30.270: E/AndroidRuntime(1628): at android.app.ActivityThread.access$600(ActivityThread.java:122)
03-20 13:47:30.270: E/AndroidRuntime(1628): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-20 13:47:30.270: E/AndroidRuntime(1628): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 13:47:30.270: E/AndroidRuntime(1628): at android.os.Looper.loop(Looper.java:137)
03-20 13:47:30.270: E/AndroidRuntime(1628): at android.app.ActivityThread.main(ActivityThread.java:4340)
03-20 13:47:30.270: E/AndroidRuntime(1628): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 13:47:30.270: E/AndroidRuntime(1628): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 13:47:30.270: E/AndroidRuntime(1628): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-20 13:47:30.270: E/AndroidRuntime(1628): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-20 13:47:30.270: E/AndroidRuntime(1628): at dalvik.system.NativeStart.main(Native Method)
03-20 13:47:30.270: E/AndroidRuntime(1628): Caused by: java.lang.NullPointerException
03-20 13:47:30.270: E/AndroidRuntime(1628): at com.example.meetap.AdHome.onCreate(AdHome.java:374)
03-20 13:47:30.270: E/AndroidRuntime(1628): at android.app.Activity.performCreate(Activity.java:4465)
03-20 13:47:30.270: E/AndroidRuntime(1628): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-20 13:47:30.270: E/AndroidRuntime(1628): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
03-20 13:47:30.270: E/AndroidRuntime(1628): ... 11 more
编辑:
我在
有NullPointerException
Button close=(Button)findViewById(R.id.close);
close.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
答案 0 :(得分:0)
Button close=(Button)findViewById(R.id.close);
close.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
如果您说上面的close
按钮中的NPE为空。在初始化按钮之前,您需要setContentView(R.layout.home);
,并且需要确保home.xml
有一个ID为close的按钮。
编辑:
它在另一个xml中定义
您需要像
那样夸大自定义布局View view = infalter.inflate(R.layout.customlayout,null);
Button close=(Button)view.findViewById(R.id.close);
答案 1 :(得分:0)
你检查了你的getIntent()!= null。你有没有检查过你的关键' != null