嗨,点击按钮,我的应用程序崩溃,出现以下错误信息::
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);
}
}
}
答案 0 :(得分: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 :(得分:1)
LogCat显示bmp
在以下位置为空:
getApplicationContext().setWallpaper(bmp);
您似乎在ib
之前点击了b
。您实例化bmp
的唯一位置是onActivityResult()
,只有当用户选择了图片时才会这样。您可以在ib
中更改onActivityResult()
按钮的可见性,以便仅在有效位图使用时显示。
另请阅读Pragnani's answer他有很好的观察,但它可能不会导致此异常,因为ImageButton是ImageView的子类。