Android-空指针异常反复崩溃

Posted

技术标签:

【中文标题】Android-空指针异常反复崩溃【英文标题】:Android- Null pointer exception repeated crashing 【发布时间】:2014-02-24 00:53:48 【问题描述】:

我是这里的初学者,所以解决方案可能微不足道。我只是想学习,应用程序因null pointer exception 而崩溃,但我看不出那可能在哪里。这是日志:

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 ComponentInfocom.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_
    android:layout_ >

    <Button
        android:id="@+id/button1"
        android:layout_
        android:layout_
        android:layout_x="-5dp"
        android:layout_y="65dp"
        android:rotation="90"
        android:text="secondimage" />



    <ImageView
        android:id="@+id/image"
        android:layout_
        android:layout_
        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

    




我听了很多建议,但没有运气。

【问题讨论】:

尝试将按钮/图像移动到 onCreate。我的猜测是,当您定义按钮/图像时,目前没有视图 只有在调用 setContentView() 之后才能找到视图。 【参考方案1】:

在 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);

            
    );

【讨论】:

【参考方案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

【讨论】:

【参考方案3】:

在 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);

        
);

【讨论】:

【参考方案4】:

移动这些行:

Button button = (Button) findViewById(R.id.button1);
ImageView img = (ImageView) findViewById(R.id.image);

在你的 setContentView() 之后

您要求 Activity 在加载视图之前查找视图

【讨论】:

【参考方案5】:

改变

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);

【讨论】:

以上是关于Android-空指针异常反复崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Android LicenseChecker因空指针异常而崩溃

由于在编辑器类中设置 onClick 侦听器时出现空指针异常,Android 应用程序崩溃

LogCat 输出空指针异常,但应用程序没有崩溃

Android开发 - 处理 null 和 预防空指针异常(NullPointerException) 的一些经验

Android自定义view,空指针异常求解决

Redmi手机上的应用程序崩溃-ZygoteInit$Method空指针异常[重复]