Doglas
Doglas

Reputation: 47

Error within getExtras()?

I need to set 2 condition in the if-statement (if this or this), the first part take an item from a listview, and the other if-part get the value from the first activity if it's true then do the condition need. See the 2 codes below may will explain it better.

putExtra code

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    TextView thisTextView = (TextView)view.findViewById(R.id.listview_item_title);
    String text = (String)thisTextView.getText();

    if (text.equals("Category")) {

        Intent i = new Intent(Search.this,MainActivity.class);
        i.putExtra("sendData", 1);
        startActivity(i);           
    }
}

getExtra Code

public class MainActivity extends Activity implements OnItemClickListener{

//private boolean mainSub,manuSub = false;
private int mainSub=0;
ListView lv;
String [] list;
int mine;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv = (ListView)findViewById(R.id.listView);
    list = getResources().getStringArray(R.array.main);

    lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list));
    lv.setOnItemClickListener(this);

    Intent myIntent = getIntent();
    mine = myIntent.getExtras().getInt("sendData");
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {

    Intent i = new Intent(MainActivity.this, SwipeActivity.class);

    if (lv.getItemAtPosition(position).toString().equalsIgnoreCase("Search")){
        i = new Intent(MainActivity.this, Search.class);
        startActivity(i);
    } 
    else if (lv.getItemAtPosition(position).toString().equalsIgnoreCase("Category")  || mine == 1) {
        lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.Category)));
        mainSub = 1;
 }

I gives the to parameters of putExtra(name,value) as name=sendData value=1 . then when I'm trying to get the Intent then the program gives an Exception and not started. I know that the error is in in the getExtras line --> mine = myIntent.getExtras().getInt("sendData"); but actually I don't know why it seems to be an error. because I think that every think is fine with coding!.

LogCat

05-02 18:06:05.218: D/SecWifiDisplayUtil(11595): Metadata value : none
05-02 18:06:05.293: D/PhoneWindow(11595): *FMB* installDecor mIsFloating : false
05-02 18:06:05.293: D/PhoneWindow(11595): *FMB* installDecor flags : 8454400
05-02 18:06:05.298: D/AbsListView(11595): Get MotionRecognitionManager
05-02 18:06:05.303: E/MotionRecognitionManager(11595): mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@8bebba7
05-02 18:06:05.303: E/MotionRecognitionManager(11595): motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@208cf854
05-02 18:06:05.303: E/MotionRecognitionManager(11595): motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@208cf854
05-02 18:06:05.308: D/AndroidRuntime(11595): Shutting down VM
05-02 18:06:05.308: E/AndroidRuntime(11595): FATAL EXCEPTION: main
05-02 18:06:05.308: E/AndroidRuntime(11595): Process: com.example.shaymatest, PID: 11595
05-02 18:06:05.308: E/AndroidRuntime(11595): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shaymatest/com.example.shaymatest.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
05-02 18:06:05.308: E/AndroidRuntime(11595):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3149)
05-02 18:06:05.308: E/AndroidRuntime(11595):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3248)
05-02 18:06:05.308: E/AndroidRuntime(11595):    at android.app.ActivityThread.access$1000(ActivityThread.java:197)
05-02 18:06:05.308: E/AndroidRuntime(11595):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1681)
05-02 18:06:05.308: E/AndroidRuntime(11595):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-02 18:06:05.308: E/AndroidRuntime(11595):    at android.os.Looper.loop(Looper.java:145)
05-02 18:06:05.308: E/AndroidRuntime(11595):    at android.app.ActivityThread.main(ActivityThread.java:6872)
05-02 18:06:05.308: E/AndroidRuntime(11595):    at java.lang.reflect.Method.invoke(Native Method)
05-02 18:06:05.308: E/AndroidRuntime(11595):    at java.lang.reflect.Method.invoke(Method.java:372)
05-02 18:06:05.308: E/AndroidRuntime(11595):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
05-02 18:06:05.308: E/AndroidRuntime(11595):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
05-02 18:06:05.308: E/AndroidRuntime(11595): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference
05-02 18:06:05.308: E/AndroidRuntime(11595):    at com.example.shaymatest.MainActivity.onCreate(MainActivity.java:35)
05-02 18:06:05.308: E/AndroidRuntime(11595):    at android.app.Activity.performCreate(Activity.java:6550)
05-02 18:06:05.308: E/AndroidRuntime(11595):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
05-02 18:06:05.308: E/AndroidRuntime(11595):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3102)
05-02 18:06:05.308: E/AndroidRuntime(11595):    ... 10 more
05-02 18:06:05.368: I/Process(11595): Sending signal. PID: 11595 SIG: 9

Upvotes: 0

Views: 809

Answers (3)

Angel Koh
Angel Koh

Reputation: 13525

There are a few ways that an activity can be started.

1) started by a launcher (you can verify this in your manifest file)

2) started by an intent.

when it is started by a launcher, then mIntent is null. trying to retrieve extras from it will result in NullPointerException.

My assumption is that MainActivity is also your launcher activity. if so, then you should check for mIntent == null before using it.

Upvotes: 0

OneCricketeer
OneCricketeer

Reputation: 191821

Your code is going to crash when there is no Intent for MainActivity that has extras (such as when you first start the app).

Therefore, you need to check if there are extras to get.

Bundle extras = getIntent().getExtras();
if (extras != null) {
    mine = extras.getInt("sendData");
}

Alternatively, you could use getIntent().getIntExtra("sendData", -1) where -1 is some default value.

Upvotes: 1

AgileNinja
AgileNinja

Reputation: 944

Instead of using getExtras().getInt use getIntExtra

Upvotes: 0

Related Questions