-1

I am learning android development and i have a problem displaying contact detail in my app.

When I click an item of contact activity, the app hangs.

Could you help me please?

This is the activity App of Contact List.

public class ListViewActivity3 extends AppCompatActivity {

    ListView listView;
    List<ContactHelper> listAdapter;
    public static final String Key_Postion = "keyPosition" ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view3);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });



        listAdapter = new ArrayList<ContactHelper>();
        listAdapter.clear();


        listAdapter.add(new ContactHelper("achref", "Tunisien", "2222222", R.drawable.imagesand));
        listAdapter.add(new ContactHelper("achref", "Tunisien", "2222222", R.drawable.imagesand));
        listAdapter.add(new ContactHelper("achref", "Tunisien", "2222222", R.drawable.imagesand));
        listAdapter.add(new ContactHelper("achref", "Tunisien", "2222222", R.drawable.imagesand));
        listAdapter.add(new ContactHelper("achref", "Tunisien", "2222222", R.drawable.imagesand));



        listView = (ListView) findViewById(R.id.listviewCustomAdapter);
        listView.setAdapter(new AdapterCustom(ListViewActivity3.this, R.layout.listviewitem1, listAdapter));



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

                Intent i3 = new Intent(ListViewActivity3.this, ContactDetailItem.class);
                i3.putExtra(Key_Postion, position);
                startActivity(i3);
            }
        });
    }
}

and this the ConatctDetailItem

public class ContactDetailItem extends AppCompatActivity {

    TextView n, ph, surn;
    ImageView imgC;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_detail_item);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        n = (TextView) findViewById(R.id.contactName);
                surn = (TextView) findViewById(R.id.contactSurname);
        ph = (TextView) findViewById(R.id.contactPhone);
        imgC = (ImageView) findViewById(R.id.imgContact);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            ContactHelper contactHelper = new ContactHelper();
            ListViewActivity3 lv = new ListViewActivity3();
            int position = bundle.getInt(ListViewActivity3.Key_Postion);
            n.setText(lv.listAdapter.get(position).getName());
            surn.setText(lv.listAdapter.get(position).getSurname());
            ph.setText(lv.listAdapter.get(position).getPhone());
            imgC.setImageResource(lv.listAdapter.get(position).getPhoto());

        }
    }
}

Here is the logcat.

com.example.achre.activityapp E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.achre.activityapp/com.example.achre.activityapp.ContactDetailItem}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at com.example.achre.activityapp.ContactDetailItem.onCreate(ContactDetailItem.java:50)
    at android.app.Activity.performCreate(Activity.java:5104)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    at android.app.ActivityThread.access$600(ActivityThread.java:141)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5041)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    at dalvik.system.NativeStart.main(Native Method) 
wottle
  • 13,095
  • 4
  • 27
  • 68
achref05
  • 1,653
  • 4
  • 16
  • 18
  • 2
    Include logcat stacktrace – wvdz Mar 23 '16 at 16:43
  • You are creating a new (empty) ListViewActivity3, which is why nothing is there. You should pass all the data you need for the contact in the extras of the Intent. – Clayton Wilkinson Mar 23 '16 at 16:49
  • ListViewActivity3 is a activity of list of contact and i instancieted it to get the list of adapter – achref05 Mar 23 '16 at 17:09
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Ferrybig Mar 24 '16 at 10:15

1 Answers1

0

In ContactDetailItem you are creating new ListViewActivity3. Instead it put to extra ContactHelper object.

Change your code like this:

Intent i3 = new Intent(ListViewActivity3.this, ContactDetailItem.class);
i3.putExtra(Key_Postion, listAdapter.get(position));
startActivity(i3);

and in ContactDetailItem:

ContactHelper contactHelper = (ContactHelper) bundle.getSerializableExtra(ListViewActivity3.Key_Postion);
n.setText(contactHelper.getName());
// ...

also your ContactHelper class must implement Serializable interface