2016-09-16 63 views
-2

我在嘗試創建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)  
+0

任何時候你有崩潰,你應該從LogCat抓取堆棧跟蹤並首先檢查它。 – kcoppock

+0

剛剛編輯我的問題與logcat,抱歉的錯誤,請現在看看,謝謝。 –

回答

0

當你startActivityForResult,你需要重寫方法onActivityResult處理響應。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
// Check which request we're responding to 
if (requestCode == PICK_CONTACT_REQUEST) { 
    // Make sure the request was successful 
    if (resultCode == RESULT_OK) { 
     // The user picked a contact. 
     // The Intent's data Uri identifies which contact was selected. 

     // Do something with the contact here (bigger example below) 
    } 
} 
} 
+0

第一次忘記添加logcat,你能再看看嗎? –

+0

ArrayAdapter itemsAdapter = new ArrayAdapter (this,android.R.layout.simple_list_item_1,values);像這樣定義適配器,然後分配給listView。 –

+0

沒有,你試着重寫onActivityResult方法嗎?同樣的結果 –

1

您在

com.isudip.task3.MainActivity.onCreate(MainActivity.java:48) 

在48號線有一個NullPointerException,你用你的ListView:

listView.setAdapter(adapter); 

這意味着,ListView控件爲空(未初始化)。您嘗試通過從佈局得到它的初始化:

setContentView(R.layout.activity_main); 
... 
ListView listView; 
listView = (ListView) findViewById(R.id.team_list); 

這似乎與ID team_list ListView控件不能在名爲activity_main佈局可以發現,作爲findViewById可能在這種情況下返回null。

檢查佈局中是否存在名爲team_list的ListView。

+0

這是有道理的,我的ListView是在另一個XML文件,我很愚蠢,從我的MainActivity調用它。感謝噸,我把與我的ListView相關的代碼移動到該Java類,一切工作正常... –

+0

@SudipBiswas,請考慮將此答案標記爲接受的答案 –

0

感謝大家的幫助。問題非常愚蠢。我試圖從我的MainActivity調用ListView,但是listview在另一個xml中,這就是爲什麼問題出在那裏,現在將我的listview的代碼移動到相關的java類,現在一切都很好。

相關問題