onActivityResult没有被调用也Logcat无法正常工作

时间:2013-07-31 14:54:50

标签: android android-activity

我在从Activity中获取结果时遇到一些麻烦。 我使用了startActivityForResult(),但是当我在第二个活动中调用finish()时,我的应用 崩溃!

MainActivity:

public class MainActivity extends Activity {

public static final int SET_IMAGE = 2;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == RESULT_OK){
        if(requestCode == SET_IMAGE){
            int position;
            position = (Integer) data.getExtras().get("CurrentImage");


            ImageView currentImage = (ImageView) findViewById(R.id.imageView);  


            currentImage.setImageResource(ImageAdapter.images[position]);

            WallpaperManager wpm = WallpaperManager.getInstance(this);
            try {
                wpm.setResource(ImageAdapter.images[position]);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Toast.makeText(getApplicationContext(), "WALL PAPER SET!! :b", Toast.LENGTH_LONG).show();
        }

    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    ImageView iv = new ImageView(this);
    Button changeButton = new Button(this);
    Button callButton = new Button(this);

    changeButton.setText("Change Picture");
    changeButton.setTextSize(20);

    callButton.setText("Call Me ;)");
    callButton.setTextSize(20);


    iv.setImageResource(ImageAdapter.images[0]);
    LinearLayout layout = new LinearLayout(this);
    LinearLayout Hlayout = new LinearLayout(this);

    Hlayout.addView(changeButton);
    Hlayout.addView(callButton);
    Hlayout.setOrientation(LinearLayout.HORIZONTAL);

    layout.addView(iv);
    layout.addView(Hlayout);

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.gravity = Gravity.CENTER;
    params.setMargins(50, 50, 50, 50);

    iv.setLayoutParams(params);
    changeButton.setLayoutParams(params);
    callButton.setLayoutParams(params);



    LinearLayout.LayoutParams LinearLayoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    layout.setLayoutParams(LinearLayoutParams);
    params.gravity = Gravity.CENTER;
    layout.setOrientation(LinearLayout.VERTICAL);



    changeButton.setOnClickListener(myhandler);
    callButton.setOnClickListener(callHandler);

    setContentView(layout);

}
View.OnClickListener callHandler = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:8307651730"));
        startActivity(callIntent);

    }
};

View.OnClickListener myhandler = new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getBaseContext(), choosePic.class);
        startActivityForResult(intent, SET_IMAGE);

    }
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

} `

在我的第二项活动中:

public void finishActivity(int position){
    Intent resultIntent = new Intent(this, MainActivity.class);


    resultIntent.putExtra("CurrentImage", position);
    setResult(RESULT_OK, resultIntent);
    finish();


}

我已将问题缩小到finish()。评论finish()会使应用运行良好,但当调用finish()时,应用会崩溃。任何人都知道为什么会这样吗?

CNC中 onActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    Toast.makeText(getApplicationContext(), "onActivityResult Called!!", Toast.LENGTH_LONG).show();
    if(resultCode == RESULT_OK){
        if(requestCode == SET_IMAGE){
            int position;
            position = (Integer) data.getExtras().get("CurrentImage");

            ImageView currentImage = (ImageView) findViewById(R.id.imageView);
            currentImage.setImageResource(ImageAdapter.images[position]);

            WallpaperManager wpm = WallpaperManager.getInstance(this);
            try {
                wpm.setResource(ImageAdapter.images[position]);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Toast.makeText(getApplicationContext(), "WALL PAPER SET!! :b", Toast.LENGTH_LONG).show();
        }

    }

}

CNC中 继承我的Logcat(最后大声笑):

  thread exiting with uncaught exception (group=0x40a71930)
 E/AndroidRuntime(667): FATAL EXCEPTION: main
 E/AndroidRuntime(667): java.lang.RuntimeException: Failure delivering result        ResultInfo{who=null, request=2, result=-1, data=Intent { (has extras) }} to activity {com.mrsai.profilepic/com.mrsai.profilepic.MainActivity}: java.lang.NullPointerException
 E/AndroidRuntime(667):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3319)
 E/AndroidRuntime(667):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
 E/AndroidRuntime(667):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
 E/AndroidRuntime(667):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
 E/AndroidRuntime(667):     at android.os.Handler.dispatchMessage(Handler.java:99)
 E/AndroidRuntime(667):     at android.os.Looper.loop(Looper.java:137)
 E/AndroidRuntime(667):     at android.app.ActivityThread.main(ActivityThread.java:5041)
 E/AndroidRuntime(667):     at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(667):     at java.lang.reflect.Method.invoke(Method.java:511)
 E/AndroidRuntime(667):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 E/AndroidRuntime(667):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 E/AndroidRuntime(667):     at dalvik.system.NativeStart.main(Native Method)
 E/AndroidRuntime(667): Caused by: java.lang.NullPointerException
 E/AndroidRuntime(667):     at com.mrsai.profilepic.MainActivity.onActivityResult(MainActivity.java:34)
 E/AndroidRuntime(667):     at android.app.Activity.dispatchActivityResult(Activity.java:5293)
 E/AndroidRuntime(667):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
 E/AndroidRuntime(667):     ... 11 more
 Sending signal. PID: 667 SIG: 9

解决 *

解决了我的问题我在xml布局中没有引用任何内容,因为我没有给它充气。 Codemagic让我意识到这一点:]谢谢男人

1 个答案:

答案 0 :(得分:0)

我认为问题在于您正在创建MainActivity此处

的新实例
Intent resultIntent = new Intent(this, MainActivity.class);

将您的代码更改为

public void finishActivity(int position){
    Intent resultIntent = new Intent();

    resultIntent.putExtra("CurrentImage", position);
    setResult(RESULT_OK, resultIntent);
    finish();

}

当您以这种方式创建新实例时,将调用onCreate()而不是onActivityResult()。使用空构造函数,然后onActivityResult()之后将调用setResult()

Docs