我正在尝试使用DataBase Sqlite在主页中显示图像。然后我在ImageView上获得空指针异常。
堆栈跟踪:
09-23 19:06:49.112: E/AndroidRuntime(979): FATAL EXCEPTION: main
09-23 19:06:49.112: E/AndroidRuntime(979): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loginregisterwithsqlite/com.example.loginregisterwithsqlite.Home}: java.lang.NullPointerException
09-23 19:06:49.112: E/AndroidRuntime(979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-23 19:06:49.112: E/AndroidRuntime(979): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-23 19:06:49.112: E/AndroidRuntime(979): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-23 19:06:49.112: E/AndroidRuntime(979): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-23 19:06:49.112: E/AndroidRuntime(979): at android.os.Handler.dispatchMessage(Handler.java:99)
09-23 19:06:49.112: E/AndroidRuntime(979): at android.os.Looper.loop(Looper.java:123)
09-23 19:06:49.112: E/AndroidRuntime(979): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-23 19:06:49.112: E/AndroidRuntime(979): at java.lang.reflect.Method.invokeNative(Native Method)
09-23 19:06:49.112: E/AndroidRuntime(979): at java.lang.reflect.Method.invoke(Method.java:507)
09-23 19:06:49.112: E/AndroidRuntime(979): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-23 19:06:49.112: E/AndroidRuntime(979): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-23 19:06:49.112: E/AndroidRuntime(979): at dalvik.system.NativeStart.main(Native Method)
09-23 19:06:49.112: E/AndroidRuntime(979): Caused by: java.lang.NullPointerException
09-23 19:06:49.112: E/AndroidRuntime(979): at com.example.loginregisterwithsqlite.Home.onCreate(Home.java:22)
09-23 19:06:49.112: E/AndroidRuntime(979): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-23 19:06:49.112: E/AndroidRuntime(979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-23 19:06:49.112: E/AndroidRuntime(979): ... 11 more
Home.java:
package com.example.loginregisterwithsqlite;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Home extends Activity{
Button button1;
ImageView imageView1;
@Override
protected void onCreate(Bundle savedInstanceState) {
button1=(Button)findViewById(R.id.button1);
imageView1=(ImageView)findViewById(R.id.imageView1);
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
button1.setOnClickListener(new OnClickListener(){ --->22nd line
@Override
public void onClick(View v) {
imageView1.setImageResource(R.drawable.android);
}
});
}
}
home.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D8D8D8" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display an Image"/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/android"
/>
</RelativeLayout>
我需要在同一个屏幕上显示图像。任何人都可以帮助我。谢谢。
答案 0 :(得分:6)
移动
button1=(Button)findViewById(R.id.button1);
imageView1=(ImageView)findViewById(R.id.imageView1);
后
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
在
之前调用setContentView之后,视图将成为活动视图层次结构的一部分答案 1 :(得分:0)
只有在主视图准备就绪后才能生成子视图。 使用setContentView()函数设置主视图。只有在此之后,您才可以使用findViewById()
创建子视图所以在setContentView()
之后移动button1和imageView1像
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
button1=(Button)findViewById(R.id.button1);
imageView1=(ImageView)findViewById(R.id.imageView1);