我是初学者,所以解决方案可能是微不足道的。我只是想学习并且应用程序崩溃了null pointer exception
,但我看不出它可能在哪里。这是logcat:
01-31 05:36:30.434: D/AndroidRuntime(1824): Shutting down VM
01-31 05:36:30.434: W/dalvikvm(1824): threadid=1: thread exiting with uncaught exception (group=0xb3aceb90)
01-31 05:36:30.454: E/AndroidRuntime(1824): FATAL EXCEPTION: main
01-31 05:36:30.454: E/AndroidRuntime(1824): Process: com.thecloset, PID: 1824
01-31 05:36:30.454: E/AndroidRuntime(1824): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.thecloset/com.thecloset.MainActivity}: java.lang.NullPointerException
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.os.Handler.dispatchMessage(Handler.java:102)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.os.Looper.loop(Looper.java:137)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.ActivityThread.main(ActivityThread.java:4998)
01-31 05:36:30.454: E/AndroidRuntime(1824): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 05:36:30.454: E/AndroidRuntime(1824): at java.lang.reflect.Method.invoke(Method.java:515)
01-31 05:36:30.454: E/AndroidRuntime(1824): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-31 05:36:30.454: E/AndroidRuntime(1824): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-31 05:36:30.454: E/AndroidRuntime(1824): at dalvik.system.NativeStart.main(Native Method)
01-31 05:36:30.454: E/AndroidRuntime(1824): HERE!!!!!!! Caused by: java.lang.NullPointerException HERE!!!!!
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.Activity.findViewById(Activity.java:1883)
01-31 05:36:30.454: E/AndroidRuntime(1824): at com.thecloset.MainActivity.<init>(MainActivity.java:16)
01-31 05:36:30.454: E/AndroidRuntime(1824): at java.lang.Class.newInstanceImpl(Native Method)
01-31 05:36:30.454: E/AndroidRuntime(1824): at java.lang.Class.newInstance(Class.java:1208)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
01-31 05:36:30.454: E/AndroidRuntime(1824): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
01-31 05:36:30.454: E/AndroidRuntime(1824): ... 11 more
继承人xml:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="-5dp"
android:layout_y="65dp"
android:rotation="90"
android:text="secondimage" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="53dp"
android:layout_y="0dp"
android:src="@drawable/im1" />
</AbsoluteLayout>
和Java:
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity implements View.OnClickListener {
Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thecloset);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
img.setImageResource(R.drawable.image2);
}
});
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}
我遵循了很多建议,但没有运气。
答案 0 :(得分:5)
在onCreate()
中的setContentView
之后移动以下代码
Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);
所以,你的班级必须像,
public class MainActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thecloset);
Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
img.setImageResource(R.drawable.image2);
}
});
答案 1 :(得分:2)
在布局膨胀之前,你正在布局中搜索一个id(因为在类初始化期间没有在方法内部完成),将findViewById调用移动到你的onCreate方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thecloset);
Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);
// the rest of your code
}
答案 2 :(得分:2)
在onCreate()方法之前找不到UI元素的id ...所以你必须在onCreate()方法中找到它。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thecloset);
Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
img.setImageResource(R.drawable.image2);
}
});
答案 3 :(得分:2)
移动以下行:
Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);
到你的setContentView()
之后您要求活动在加载视图之前查找视图
答案 4 :(得分:2)
更改
Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thecloset);
到
Button button; = (Button) findViewById(R.id.button1);
ImageView img; = (ImageView) findViewById(R.id.image);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thecloset);
button = (Button) findViewById(R.id.button1);
img = (ImageView) findViewById(R.id.image);