我遇到了这个问题Unable to start activity ComponentInfo{…}: java.lang.NullPointerException
,但我不知道在哪里。
我是androïd的初学者,我无法摆脱这个错误..
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)
的AndroidManifest.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";
}
}
}
如果有人对此有所了解:)
答案 0 :(得分:0)
如果您要使用startActivityForResult()
,则必须在调用Activity
中放置/覆盖此方法:
protected void onActivityResult(int requestCode, int resultCode, Intent data){}
在您的情况下,MainActivity
。然后在被叫Activity
中,您使用Intent
使用setResult(RESULT_OK)
开始了。此方法会将resultCode
返回到您的调用Activity
onActivityResult
方法。然后在方法体测试中:
if(resultCode == RESULT_OK)
{
// Do the stuff you want
}
答案 1 :(得分:0)
http://developer.android.com/guide/components/activities.html刚刚阅读了有关startActivityForResult()
的部分