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:
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;
}
} `
In my 2nd activity:
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:
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- 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*
Fixed my problem i was referencing nothing in my xml layout because i didnt inflate it. Codemagic made me realize this :] thanks man