Gioele
Gioele

Reputation: 83

Android: can't display a toast

I have a problem to display a toast instead of another toast if already been viewed. I have two activity, in the first Activity toast is displayed and i have this code:

@Override
 protected void onCreate(Bundle savedInstanceState) {
 // TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 setContentView(R.layout.q_050);
 String str = getIntent().getStringExtra("sent");
// at this line--> if(str.equals("activity_two")){
     Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.app_name), Toast.LENGTH_SHORT).show();
 }
 else{
     Toast.makeText(getApplicationContext(), getApplicationContext().getString(R.string.checkpoint), Toast.LENGTH_SHORT).show();
 }

in the second activity in the back button I included this:

  Button b3 = (Button)findViewById(R.id.b3);
                b3.setOnClickListener(new Button.OnClickListener() {  
                       public void onClick(View v){

                           Intent myIntentActivity1 = new Intent(Q_051.this,Q_050.class);
                           myIntentActivity1.putExtra("sent", "activity_two");
                           startActivity(myIntentActivity1);

and the problem in logcat is this :

 11-09 16:29:51.920: E/AndroidRuntime(1510): FATAL EXCEPTION: main
11-09 16:29:51.920: E/AndroidRuntime(1510): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.quizcelebrita/org.quizcelebrita.Q_050}: java.lang.NullPointerException
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.os.Looper.loop(Looper.java:137)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.ActivityThread.main(ActivityThread.java:4898)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at java.lang.reflect.Method.invoke(Method.java:511)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at dalvik.system.NativeStart.main(Native Method)
11-09 16:29:51.920: E/AndroidRuntime(1510): Caused by: java.lang.NullPointerException
11-09 16:29:51.920: E/AndroidRuntime(1510):     at org.quizcelebrita.Q_050.onCreate(Q_050.java:32)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.Activity.performCreate(Activity.java:5206)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
11-09 16:29:51.920: E/AndroidRuntime(1510):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
11-09 16:29:51.920: E/AndroidRuntime(1510):     ... 11 more

the line where the problem is detected is this:

   if(str.equals("activity_two")){

Upvotes: 1

Views: 211

Answers (2)

Rahul Gupta
Rahul Gupta

Reputation: 5295

You are getting a null pointer exception on the if condition you mentioned Try debugging your application and put a breakpoint on this line :-

String str = getIntent().getStringExtra("sent");

Check if str is getting any value or not. My guess that it is not receiving any string so that's why it is showing null pointer exception

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157437

Change

 Bundle value = getIntent().getExtras();
 String str= value.getString("sent").toString();

in

String str = getIntent().getStringExtra("sent");

also, String comparison in Java should be performed through the equals method

if(str.equals("activity_two"))

Upvotes: 2

Related Questions