-1

In my apps I want to pass some string data from one class to another class using intent class .I use the Following putExtra to send data to another class named BusDetail .

        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
        {

            if(groupPosition==0)
            {
                if(childPosition == 0)
                {
                    Intent child0Intent = new Intent(getBaseContext(), BusDetail.class);
                    child0Intent.putExtra("ROOT",root_name[0]);
                    startActivity(child0Intent);
                }

            }

            if(groupPosition==1)
            {
                if(childPosition == 0)
                {
                    Intent child0Intent = new Intent(getBaseContext(), BusDetail.class);
                    child0Intent.putExtra("ROOT",root_name[0]);
                    startActivity(child0Intent);
                }

            }
            return false;
        }
    });
}

The following is BusDetail class where I use getStringExtra to collect data from the above class . But my app crashes when I go to above class to following class

final Intent intent=getIntent();
//passedArg=in

final String id = intent.getStringExtra("ROOT");

Without using putExtra and getStringExtra, my app works .

this is my logcat

01-19 15:22:23.078  22686-22686/com.example.kuet.traveldirectory E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.kuet.traveldirectory, PID: 22686
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.kuet.traveldirectory/com.example.kuet.traveldirectory.BusDetail}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference
        at com.example.kuet.traveldirectory.BusDetail.<init>(BusDetail.java:32)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.Class.newInstance(Class.java:1606)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)

2 Answers2

-1

you should try with,

final String id = intent.getExtras().getString("ROOT");
Htoo Aung Hlaing
  • 2,173
  • 15
  • 26
-1

In your current Activity, create a new Intent:

Intent child0Intent = new Intent(getBaseContext(), BusDetail.class);
               child0Intent.putExtra("ROOT",root_name[0]);
                startActivity(child0Intent);

Then in the new Activity, retrieve those values:

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value = extras.getString("ROOT");
}

Use this technique to pass variables from one Activity to the other.

Suhas Bachewar
  • 1,230
  • 7
  • 21