0

I got this problem Unable to start activity ComponentInfo{…}: java.lang.NullPointerException but i don't know where. I'm a beginner in androïd and i can't get out of this error..

Logcat:

05-29 02:37:57.280    2030-2030/com.example.myapplication2.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication2.app/com.example.myapplication2.app.Activity_detail_dvd}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.myapplication2.app.Activity_detail_dvd.onCreate(Activity_detail_dvd.java:45)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)

AndroïdManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication2.app" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.myapplication2.app.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Activity_detail_dvd"
            android:label="@string/title_activity_activity_detail_dvd" >
        </activity>
    </application>

</manifest>

MainActivity.java:

public class MainActivity extends Activity {
    private static final int CODE_DE_MON_ACTIVITE = 1;
    private List<Dvd> myDvds = new ArrayList<Dvd>();

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

        populateDvdList();
        populateListView();
        registerClickCallback();
    }

    private void populateDvdList() {
        myDvds.add(new Dvd("Shrek", 2004, R.drawable.shrek, "A voir"));
        myDvds.add(new Dvd("alibaba", 2024, R.drawable.shrek, "Nul"));
        myDvds.add(new Dvd("Test", 20014, R.drawable.shrek, "A voir"));
        myDvds.add(new Dvd("Munster", 2014, R.drawable.shrek, "A voir"));
        myDvds.add(new Dvd("E.T", 2024, R.drawable.shrek, "A voir"));
        myDvds.add(new Dvd("Gangs of America", 1999, R.drawable.shrek, "A voir"));
    }

    private void populateListView() {
        ArrayAdapter<Dvd> adapter = new MyListAdapter();
        ListView list = (ListView) findViewById(R.id.dvdListView);
        list.setAdapter(adapter);
    }

    private class MyListAdapter extends ArrayAdapter<Dvd> {

        public MyListAdapter() {
            super(MainActivity.this, R.layout.item_view, myDvds);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            //On s'assure d'avoir une vue pour travailler avec
            View itemView = convertView;
            if (itemView == null) {
                itemView = getLayoutInflater().inflate(R.layout.item_view, parent, false);
            }
            Dvd currentDvd = myDvds.get(position);

            ImageView imageView = (ImageView) itemView.findViewById(R.id.item_icon);
            imageView.setImageResource(currentDvd.getIconID());

            TextView makeText = (TextView) itemView.findViewById(R.id.item_textMake);
            makeText.setText(currentDvd.getMake());

            TextView yearText = (TextView) itemView.findViewById(R.id.item_textYear);
            yearText.setText("" + currentDvd.getYear());

            TextView conditionText = (TextView) itemView.findViewById(R.id.item_textCondition);
            conditionText.setText(currentDvd.getCondition());

            return itemView;
        }
    }

    private void registerClickCallback() {
        ListView list = (ListView) findViewById(R.id.dvdListView);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id){
                Dvd clickedDvd = myDvds.get(position);

                Bundle objetbunble = new Bundle();

                objetbunble.putString("titre", clickedDvd.getMake());
                objetbunble.putString("description", String.valueOf(clickedDvd.getYear()));

                Intent intent = new Intent(MainActivity.this, Activity_detail_dvd.class);

                intent.putExtras(objetbunble);

                startActivityForResult(intent, CODE_DE_MON_ACTIVITE);
            }
        });
    }
}

Activity_detail_dvd.java:

public class Activity_detail_dvd extends Activity {
    private static final int CODE_DE_MON_ACTIVITE = 2;
    private String titre;
    private String description;

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

        setContentView(R.layout.activity_activity_detail_dvd);

        TextView textView = (TextView)findViewById(R.id.list_item);

        Bundle objetbunble  = this.getIntent().getExtras();

        if (objetbunble != null && objetbunble.containsKey("titre") && objetbunble.containsKey("description")) {
            titre = this.getIntent().getStringExtra("titre");
            description = this.getIntent().getStringExtra("description");
        }else {
            titre = "Error";
            description = "Error";
        }


    }

}

If someone got an idea for that :)

Fernando Carvalhosa
  • 1,098
  • 1
  • 15
  • 23
  • 1
    It says you have an NullPointerException in Activity_detail_dvd.java on line number 45. But you have total 38 lines in this activity. If you pasted full code of Activity_detail_dvd.java ? – user934820 May 29 '14 at 03:12
  • Owh sorry, It's because i removed comments. Line 45 is textView.setText("Utilisez-vous " + titre +" comme "+description+"?"); – user3264208 May 29 '14 at 03:23
  • make sure your textview name is list_item. If it is then rename it to for example txtDesc and try again. – user934820 May 29 '14 at 03:26
  • @user3264208 : First get rid of this line `import com.example.myapplication2.app.R;` - you shouldn't import your own `R.java` class unless your `Activity` is part of a different package. Second - you say line 45 is `textView.setText("Utilisez-vous " + titre +" comme "+description+"?");` but there isn't a line like that in your `Activity_detail_dvd` code. Please post exact code otherwise nobody can help. – Squonk May 29 '14 at 03:38

2 Answers2

0

If you are going to use startActivityForResult() you have to put/override this method in the calling Activity:

protected void onActivityResult(int requestCode, int resultCode, Intent data){}

Which, in your case, is MainActivity. Then in the called Activity you started up using your Intent use setResult(RESULT_OK). This method will return that resultCode to your calling Activity's onActivityResult method. Then in the method body test:

if(resultCode == RESULT_OK)
{
   // Do the stuff you want
}
Leigh
  • 12,038
  • 4
  • 28
  • 36
yup
  • 16
  • 1
0

http://developer.android.com/guide/components/activities.html just read the section on startActivityForResult()

yup
  • 16
  • 1