0

我可以点击图片,但图片没有出现在 imageview 中,并且点击按钮结束

public class Camera extends Activity implements View.OnClickListener{

ImageView iv;
ImageButton ib;
Button b;
Intent i;
final static int cameradata=0;

Bundle extras;
Bitmap bmp;



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
  setContentView(R.layout.photo);

iv=(ImageView)findViewById(R.id.ivReturnedpic);
ib=(ImageButton)findViewById(R.id.ibTakepic);
b=(Button)findViewById(R.id.bSetwall);
b.setOnClickListener(this);
ib.setOnClickListener(this);



}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.ibTakepic:
    Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(i,cameradata);


    break;

    case R.id.bSetwall:
        try {
            getApplicationContext().setWallpaper(bmp);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }         
        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 extras = data.getExtras();
    Bitmap bmp=(Bitmap) extras.get("data");
    }



}
 }

原木猫

06-09 01:30:15.898: D/AndroidRuntime(1602):
Shutting down VM 06-09 01:30:15.898: W/dalvikvm(1602):
 threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-09 01:30:15.945: E/AndroidRuntime(1602): FATAL EXCEPTION: main
06-09 01:30:15.945: E/AndroidRuntime(1602): java.lang.NullPointerException
06-09 01:30:15.945: E/AndroidRuntime(1602):     at android.app.WallpaperManager.setBitmap(WallpaperManager.java:501)
06-09 01:30:15.945: E/AndroidRuntime(1602):     at android.app.ContextImpl.setWallpaper(ContextImpl.java:616)
06-09 01:30:15.945: E/AndroidRuntime(1602):     at android.content.ContextWrapper.setWallpaper(ContextWrapper.java:243)
06-09 01:30:15.945: E/AndroidRuntime(1602):     at com.example.bucky1.Camera.onClick(Camera.java:55)
06-09 01:30:15.945: E/AndroidRuntime(1602):     at android.view.View.performClick(View.java:2485)
06-09 01:30:15.945: E/AndroidRuntime(1602):     at android.view.View$PerformClick.run(View.java:9080)
06-09 01:30:15.945: E/AndroidRuntime(1602):     at android.os.Handler.handleCallback(Handler.java:587)
06-09 01:30:15.945: E/AndroidRuntime(1602):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-09 01:30:15.945: E/AndroidRuntime(1602):     at android.os.Looper.loop(Looper.java:130)
06-09 01:30:15.945: E/AndroidRuntime(1602):     at android.app.ActivityThread.main(ActivityThread.java:3687)
06-09 01:30:15.945: E/AndroidRuntime(1602):     at java.lang.reflect.Method.invokeNative(Native Method)
06-09 01:30:15.945: E/AndroidRuntime(1602):     at java.lang.reflect.Method.invoke(Method.java:507)
06-09 01:30:15.945: E/AndroidRuntime(1602):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-09 01:30:15.945: E/AndroidRuntime(1602):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-09 01:30:15.945: E/AndroidRuntime(1602):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

只需使用类级别变量bmp

@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 extras = data.getExtras();
    this.bmp=(Bitmap) extras.get("data");
}
于 2013-06-08T21:52:55.143 回答