2

I am trying to pass intent extras to the same activity, but giving null pointer exception when clicking the page. I am trying to take an intent extra and use that to determine which layout file to load.

public class stuff extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        if(extras !=null){
            String value = extras.getString("name");
            if(value.equals("all")){
                setContentView(R.layout.allnames);
            }
            if(value.equals("steve")){
                setContentView(R.layout.steve);
            }
            if(value.equals("mark")){
                setContentView(R.layout.mark);
            }
        }

        Button btnSteve = (Button) findViewById(R.id.btnSteve);
        btnSteve.setOnClickListener(new View.OnClickListener() {    
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), stuff.class);
                myIntent.putExtra("name", "steve");
                startActivityForResult(myIntent, 0);
    }
        });
    }
}

Logcat is giving a

Unable to start activity ComponentInfo() java.lang.NullPointerException

when button is pressed. I am fairly new to Android development and I have searched online, and I am unable to find any information about passing intent extras back to same activity.

Have declared this activity in the manifest as well.

        <intent-filter>
            <action android:name=".stuff" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

This is the logcat errors:

06-20 09:00:48.817: E/AndroidRuntime(32586): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.appname.com/app.appname.com.Stuff}: java.lang.NullPointerException
06-20 09:00:48.817: E/AndroidRuntime(32586):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136)
06-20 09:00:48.817: E/AndroidRuntime(32586):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174)
06-20 09:00:48.817: E/AndroidRuntime(32586):    at android.app.ActivityThread.access$700(ActivityThread.java:141)
06-20 09:00:48.817: E/AndroidRuntime(32586):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1267)
06-20 09:00:48.817: E/AndroidRuntime(32586):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 09:00:48.817: E/AndroidRuntime(32586):    at android.os.Looper.loop(Looper.java:137)
06-20 09:00:48.817: E/AndroidRuntime(32586):    at android.app.ActivityThread.main(ActivityThread.java:5059)
06-20 09:00:48.817: E/AndroidRuntime(32586):    at java.lang.reflect.Method.invokeNative(Native Method)
06-20 09:00:48.817: E/AndroidRuntime(32586):    at java.lang.reflect.Method.invoke(Method.java:511)
06-20 09:00:48.817: E/AndroidRuntime(32586):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
06-20 09:00:48.817: E/AndroidRuntime(32586):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
06-20 09:00:48.817: E/AndroidRuntime(32586):    at dalvik.system.NativeStart.main(Native Method)
06-20 09:00:48.817: E/AndroidRuntime(32586): Caused by: java.lang.NullPointerException
06-20 09:00:48.817: E/AndroidRuntime(32586):    at app.appname.com.Stuff.onCreate(AllClasses.java:32)
06-20 09:00:48.817: E/AndroidRuntime(32586):    at android.app.Activity.performCreate(Activity.java:5058)
06-20 09:00:48.817: E/AndroidRuntime(32586):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-20 09:00:48.817: E/AndroidRuntime(32586):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)

Any assistance would be appreciated.

Thanks,

user1443402
  • 33
  • 2
  • 9

3 Answers3

0

try like that

 public void onClick(View view) {
                    Intent myIntent = new Intent(Activity.this, stuff.class);
                    myIntent.putExtra("name", "steve");
                    startActivity(myIntent);
                }

change in menifest activity part

  <activity android:name=".stuff">
        <intent-filter>
         <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
  • getActivity() The getActivity() is undefined for the type stuff – user1443402 Jun 20 '13 at 13:55
  • in place of view.getContext(), put your activity class name like (Activity.this) – Sunil Kumar Jun 20 '13 at 13:55
  • here is the logcat for using your suggestion: – user1443402 Jun 20 '13 at 14:02
  • 06-20 09:00:48.817: E/AndroidRuntime(32586): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.name.com.stuff}: java.lang.NullPointerException 06-20 09:00:48.817: E/AndroidRuntime(32586): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2136) 06-20 09:00:48.817: E/AndroidRuntime(32586): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2174) 06-20 09:00:48.817: E/AndroidRuntime(32586): at android.app.ActivityThread.access$700(ActivityThread.java:141) – user1443402 Jun 20 '13 at 14:04
  • make sure that in menifest your package name is app.name.com and your stuff activity class in that package? – Sunil Kumar Jun 20 '13 at 14:07
  • it is, if I create a new class and pass the name extra to it, it works fine, but if trying to pass it back to itself is when the nullpointer error occurs – user1443402 Jun 20 '13 at 14:15
  • and why you calling the same activty – Sunil Kumar Jun 20 '13 at 14:24
  • Does not work either, same nullpointer error, going to just create another class and pass to it, appreciate the help – user1443402 Jun 20 '13 at 14:36
0

The problem is in the context you are passing to the intent (view.getContext()) try the following:

Button btnSteve = (Button) findViewById(R.id.btnSteve);
        btnSteve.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(YourActivity.this, stuff.class);
                myIntent.putExtra("name", "steve");
                startActivityForResult(myIntent, 0);
            }

        });

And to handle the result returned from stuff, you need to override onActivityResult

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    }
Nermeen
  • 15,883
  • 5
  • 59
  • 72
  • And where does the @override go? because getting "void is an invalid type for the variable onActivityResult" – user1443402 Jun 20 '13 at 14:08
  • I have tried it above the @Override public void onCreate(Bundle savedInstanceState) {...} and at bottom of activity and it doesn't give any errors in eclipse but still nullpointer exception. – user1443402 Jun 20 '13 at 14:20
0

Your code is OK except because you have to set a content view for the first time, when the extras are null. The exception rises because the Activity cannot find the Button view.

public class stuff extends Activity {

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

        if (getIntent().hasExtra("name")) {
            String value = getIntent().getStringExtra("name");
            ...

        } else {
            // For the first time, when Intent has no extras
            setContentView(R.layout.activity_stuff)
        }

        ...
    }
josemigallas
  • 3,761
  • 1
  • 29
  • 68