onActivityResult没有得到也称为LogCat中无法正常工作 [英] onActivityResult not getting Called Also Logcat not working Correctly

查看:93
本文介绍了onActivityResult没有得到也称为LogCat中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦,从活动中得到的结果。 我用 startActivityForResult(),但是当我打电话完成()在第二个活动,我的应用程序 崩溃!

I am having some trouble getting result from an Activity. I used startActivityForResult(), but when I call finish() in the 2nd Activity, my app crashes!

MainActivity:

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


}

我已经缩小的问题完成()。注释掉完成(),使应用程序运行正常,但当完成()被称为应用程序崩溃。任何人有任何想法,这是为什么?

I've narrowed down the problem to finish(). Commenting out finish() makes the app run fine but when finish() is called the app crashes. Anyone have any idea why that is?

-edit- onActivityResult:

-edit- 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();
        }

    }

}

-edit- 这是我的logcat的(最后笑):

-edit- Heres my Logcat (Finally lol):

  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

解决的*

Resolved*

修正了我的问题,我在我的XML布局引用什么,因为我没有它充气。 codemagic使我认识到这一点:]感谢的人

Fixed my problem i was referencing nothing in my xml layout because i didnt inflate it. Codemagic made me realize this :] thanks man

推荐答案

我认为这个问题是要创建的新实例的 MainActivity 此处

I think the problem is that you are creating a new instance of your MainActivity here

Intent resultIntent = new Intent(this, MainActivity.class);

更改code到

Change your code to

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

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

}

当你创建一个新的实例的方式,的onCreate()将被调用,而不是 onActivityResult()。使用空的构造则 onActivityResult()的setResult将被称为()

When you create a new instance that way, onCreate() will be called instead of onActivityResult(). Use the empty constructor then onActivityResult() will be called after setResult().

文档

这篇关于onActivityResult没有得到也称为LogCat中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆