-1

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

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


        }

    }

}
4

2 回答 2

1

The LogCat shows that bmp is null in:

getApplicationContext().setWallpaper(bmp);

It looks like you clicked ib before b. The only place that you instantiate bmp is in onActivityResult() and only then if the user selected an image. You can change the ib Button's visibility in onActivityResult() to only be shown when there is a valid Bitmap to use.

Also please read Pragnani's answer he has a great observation, though it might not cause this exception since ImageButton is a subclass of ImageView.

于 2013-03-26T18:49:36.870 回答
1

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

IE

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

两者具有相同的IDR.id.imageButton1

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

尝试更改imageview的ID

于 2013-03-26T18:42:54.373 回答