我在嘗試創建android listview時遇到此問題。如果我插入listview的代碼,那麼該應用程序不會打開,它不幸顯示應用程序已停止,如果我刪除該列表視圖的代碼,一切都很好。致命異常:main java.lang.RuntimeException:無法在Android Studio中啓動活動ComponentInfo
這是我的MainActivity.java文件。我也有其他文件,但沒有對它們進行任何更改,所以我認爲問題出在這個文件中。這裏有什麼問題?
package com.isudip.task3;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView;
public class MainActivity extends AppCompatActivity {
public Button btn;
//button on the start page
public void summary(){
btn = (Button)findViewById(R.id.start);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent summaryView = new Intent(MainActivity.this, Summary.class);
startActivity(summaryView);
}
});
}
//adding list
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
summary();
ListView listView;
listView = (ListView) findViewById(R.id.team_list);
String[] values = new String[]{
"Item 1",
"Item 2",
"Item 3",
"Item 4"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
Intent myIntent = new Intent(view.getContext(), City.class);
startActivityForResult(myIntent, 0);
}
if (position == 1) {
Intent myIntent = new Intent(view.getContext(), Arsenal.class);
startActivityForResult(myIntent, 0);
}
}
});
}
}
編輯:對不起,我忘了更新我的logcat的,那就是:
FATAL EXCEPTION: main
Process: com.isudip.task3, PID: 9214
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.isudip.task3/com.isudip.task3.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.isudip.task3.MainActivity.onCreate(MainActivity.java:48)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
任何時候你有崩潰,你應該從LogCat抓取堆棧跟蹤並首先檢查它。 – kcoppock
剛剛編輯我的問題與logcat,抱歉的錯誤,請現在看看,謝謝。 –