嗨,单击按钮时,我的应用程序崩溃并显示以下错误消息::
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);
}
}
}