2014-05-09 73 views
0

在使用列表視圖遭遇一個NullPointerException。 我已經初始化我的適配器,我的listview和我的課程數組以及。的NullPointerException在setadapter()mehtod

道歉,如果我的疑問,聽起來可能有點模糊。我是一個新手,Android和不能找出錯誤

AboutActivity.java

public class AboutActivity extends Activity { 

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

    setContentView(R.layout.activity_about); 

    String[] lessons = new String[] { "Android Introduction","Android Setup/Installation","Android Hello World","Android Layouts/Viewgroups","Android Activity & Lifecycle","Intents in Android"}; 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,lessons); 
    ListView listview = (ListView)findViewById(R.id.ListView); 
    listview.setAdapter(adapter); 

} 

fragment_about.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.chatclient.AboutActivity$PlaceholderFragment" > 

    <ListView 
     android:id="@+id/ListView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" > 

    </ListView> 

</RelativeLayout> 

的logcat:

05-09 01:41:59.029: E/AndroidRuntime(1579): FATAL EXCEPTION: main 
05-09 01:41:59.029: E/AndroidRuntime(1579): Process: com.example.chatclient, PID: 1579 
05-09 01:41:59.029: E/AndroidRuntime(1579): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chatclient/com.example.chatclient.AboutActivity}: java.lang.NullPointerException 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at android.os.Handler.dispatchMessage(Handler.java:102) 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at android.os.Looper.loop(Looper.java:136) 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at java.lang.reflect.Method.invoke(Method.java:515) 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at dalvik.system.NativeStart.main(Native Method) 
05-09 01:41:59.029: E/AndroidRuntime(1579): Caused by: java.lang.NullPointerException 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at com.example.chatclient.AboutActivity.onCreate(AboutActivity.java:29) 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at android.app.Activity.performCreate(Activity.java:5231) 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
05-09 01:41:59.029: E/AndroidRuntime(1579):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
05-09 01:41:59.029: E/AndroidRuntime(1579):  ... 11 more 
+0

的[什麼是空指針異常,以及如何解決?(http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how可能重複-do-i-fix-it) –

+0

你的代碼在我的最後工作正常。你能否請你重新檢查一下,讓我知道它是否仍然在你的末端崩潰? – sUndeep

+0

@Jonathon萊因哈特,這是一個非常普遍的問題,這是特定的。不是重複的,儘管修復部分可能與reverse_web要求的答案有一些重疊。 – rekaszeru

回答

1

錯誤:

在你Activity,你在呼喚:

setContentView(R.layout.activity_about); 

而創建內部fragment_about.xml你的看法這顯示佈局activity_about.xml。它使NullPointerException異常因爲系統無法找到的觀點:

findViewById(R.id.ListView); // this 'id' (R.id.ListView) cannot be find 

解決方案:

activity_about.xml創建你的意見或改變目標佈局如下:

setContentView(R.layout.fragment_about); 
+0

謝謝..經驗教訓 –

+0

嗨@reverse_web,做錯了什麼?你需要更多的解釋嗎?你似乎取消了之前接受的答案...你需要別的東西嗎? – Fllo

+0

嘿我很抱歉沒有選中以前接受的答案。一定是錯誤地發生了。 –

3

你應該改變這種

setContentView(R.layout.activity_about); 

setContentView(R.layout.fragment_about); 

這是因爲ListViewfragment_about.xml文件,你會發現ListViewactivity_about.xml文件

ListView listview = (ListView)findViewById(R.id.ListView); //Cause NULL Pointer Exception 
+1

嗨,你有沒有測試任何與「ListView」id與此android:id =「@ + id/ListView」id becz我沒有任何錯誤與這種id聲明有關,它不是強制性的,我們不能將ListVew標識設置爲「@ + id/ListView」。 –

+0

謝謝..經驗教訓 –

0

你得到空指針,因爲使用的是佔位符,即片段的UI和在活動中編寫java代碼。以下是解決方案。

1.setContentView(R.layout.fragment_about); 
2. change your activity like below: 

public class AboutActivity extends ActionBarActivity { 

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

    if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.container, new PlaceholderFragment()).commit(); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 
    ArrayAdapter<String> adapter; 
    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, 
       false); 

     String[] lessons = new String[] { "Android Introduction","Android Setup/Installation","Android Hello World","Android Layouts/Viewgroups","Android Activity & Lifecycle","Intents in Android"}; 

     adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,lessons); 
     ListView listview = (ListView)rootView.findViewById(R.id.ListView); 
     listview.setAdapter(adapter); 
     return rootView; 
    } 
} 

} 
0

嘗試更換這行:

setContentView(R.layout.activity_about); 

有了:

setContentView(R.layout.fragment_about); 

我希望這有助於!

相關問題