【问题标题】:Getting Fatal Exception获得致命异常
【发布时间】:2013-03-26 18:41:29
【问题描述】:

您好,点击按钮时我的应用程序崩溃并显示以下错误消息 ::

 W/dalvikvm(15165): threadid=1: thread exiting with uncaught exception (group=0x40018578)
 E/AndroidRuntime(15165): FATAL EXCEPTION: main
 E/AndroidRuntime(15165): java.lang.NullPointerException
 E/AndroidRuntime(15165):   at android.app.WallpaperManager.setBitmap(WallpaperManager.java:501)
 E/AndroidRuntime(15165):   at android.app.ContextImpl.setWallpaper(ContextImpl.java:616)
 E/AndroidRuntime(15165):   at android.content.ContextWrapper.setWallpaper(ContextWrapper.java:243)
 E/AndroidRuntime(15165):   at com.thenewboston.travis.Camera.onClick(Camera.java:47)
 E/AndroidRuntime(15165):   at android.view.View.performClick(View.java:2485)
 E/AndroidRuntime(15165):   at android.view.View$PerformClick.run(View.java:9080)
 E/AndroidRuntime(15165):   at android.os.Handler.handleCallback(Handler.java:587)
 E/AndroidRuntime(15165):   at android.os.Handler.dispatchMessage(Handler.java:92)
 E/AndroidRuntime(15165):   at android.os.Looper.loop(Looper.java:130)
 E/AndroidRuntime(15165):   at android.app.ActivityThread.main(ActivityThread.java:3687)
 E/AndroidRuntime(15165):   at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(15165):   at java.lang.reflect.Method.invoke(Method.java:507)
 E/AndroidRuntime(15165):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
 E/AndroidRuntime(15165):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
 E/AndroidRuntime(15165):   at dalvik.system.NativeStart.main(Native Method)

Camera.java 包 com.thenewboston.travis;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Camera extends Activity implements View.OnClickListener{

    ImageButton ib;
    ImageView iv;
    Button b;
    Intent i;
    Bitmap bmp;
    final static int cameraData = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.photo);
        initialize();
    }
    private void initialize() {
        // TODO Auto-generated method stub

        ib = (ImageButton)findViewById(R.id.imageButton1);
        iv = (ImageView)findViewById(R.id.imageButton1);
        b = (Button)findViewById(R.id.button1);

        b.setOnClickListener(this);
        ib.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId())
        {
        case R.id.imageButton1 :
            try {
                getApplicationContext().setWallpaper(bmp);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            break;
        case R.id.button1:
            i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i,cameraData);
            break;

        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK){
            Bundle ext = data.getExtras();
            bmp = (Bitmap)ext.get("data");
            iv.setImageBitmap(bmp);


        }

    }

}

【问题讨论】:

    标签: android android-camera


    【解决方案1】:

    您正在尝试获取具有相同 ID 的 ImageView 和 ImageButton

     ib = (ImageButton)findViewById(R.id.imageButton1);
     iv = (ImageView)findViewById(R.id.imageButton1);
    

    两者具有相同的 ID R.id.imageButton1

    所以你会得到你的 imageview NULL,这会导致 NullPoinerException。

    尝试改变imageview的ID

    【讨论】:

    • +1 很好,但这不会修复当前的错误。 :((你也是在引用自己的话吗?)
    • @Sam 谢谢,很高兴得到 SO 中最喜欢的人之一的赞赏
    • @Sam “你也在引用自己的话吗?”抱歉,我不知道,我对 SO 中的内容仍然知之甚少。如果我做错了,请更正
    • > 用于块引号,但您在“您正在尝试获取具有相同 ID 的 ImageView 和 ImageButton”时使用它。你是想突出这句话吗?
    • 它应该只用于引用某些东西,而不是作为强调(突出显示)。您可以在Editing Guide 中找到其他技巧。不用担心你的英语是否完美,只要你的代码是好的。我们都说安卓。 :)
    【解决方案2】:

    LogCat 显示 bmp 在以下位置为空:

    getApplicationContext().setWallpaper(bmp);
    

    看起来您在点击b 之前点击了ib。您实例化 bmp 的唯一位置是在 onActivityResult() 中,并且只有在用户选择图像的情况下。您可以更改ib 按钮在onActivityResult() 中的可见性,使其仅在有可用的有效位图时显示。

    另外请阅读Pragnani's answer 他有一个很好的观察,虽然它可能不会导致这个异常,因为 ImageButton 是 ImageView 的子类。

    【讨论】:

    • 我认为你应该得到这个答案...他可能错误地接受了我的答案...但是 +1 答案
    猜你喜欢
    • 1970-01-01
    • 2014-11-30
    • 2015-11-16
    • 2015-08-31
    • 2014-08-26
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    相关资源
    最近更新 更多