2013-02-08 28 views
1

請看看下面的代碼NullPointException ListView中

activity_form.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" 
    tools:context=".Form" > 

    <ImageView 
     android:id="@+id/logo" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:src="@drawable/logo" /> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/logo" 
     android:layout_marginTop="10dp" 
     android:text="@string/title" /> 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/title" 
     android:layout_marginTop="10dp" 
     /> 

</RelativeLayout> 

list_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/listImage" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:src="@drawable/star" /> 

    <TextView 
     android:id="@+id/listText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="5dp" 
     android:paddingTop="10dp" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</LinearLayout> 

Form.java

package com.example.callmanager; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 

public class Form extends Activity { 

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

     ListView lv = (ListView)findViewById(android.R.id.list); 
     String arr[] = getResources().getStringArray(R.array.branch_list); 
     lv.setAdapter(new MyAdapter(this,android.R.layout.simple_list_item_1,R.id.listText,arr)); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_form, menu); 
     return true; 
    } 

    private class MyAdapter extends ArrayAdapter 
    { 

     public MyAdapter(Context context, int resource, int textViewResourceId, 
       Object[] objects) { 
      super(context, resource, textViewResourceId, objects); 
      // TODO Auto-generated constructor stub 
     } 

     @Override 
     public View getView(int position, View contentView, ViewGroup parent) 
     { 
      LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); 
      View view = inflater.inflate(R.layout.list_layout, parent,false); 

      TextView tv = (TextView)findViewById(R.id.listText); 
      ImageView iv = (ImageView)findViewById(R.id.listImage); 

      String arr[] = getResources().getStringArray(R.array.branch_list); 
      tv.setText(arr[position]); 

      if(arr[position].equals("Anuradhapura")) 
      iv.setImageResource(R.drawable.star); 

      return view; 

     } 

    } 

} 

的strings.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

    <string name="app_name">Esoft Call Manager</string> 
    <string name="hello_world">Hello world!</string> 
    <string name="menu_settings">Settings</string> 
    <string name="title"><b>Select a Branch To Call</b></string> 

    <string-array name="branch_list"> 
     <item>Anuradhapura</item> 
     <item>Avissawella</item> 

    </string-array> 

</resources> 

當我運行這段代碼,我得到了 'NullPointException'。我加入這裏的完整的日誌文件

02-08 21:04:01.463: D/dalvikvm(490): GC_EXTERNAL_ALLOC freed 43K, 53% free 2551K/5379K, external 1949K/2137K, paused 82ms 
02-08 21:04:01.713: D/AndroidRuntime(490): Shutting down VM 
02-08 21:04:01.713: W/dalvikvm(490): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
02-08 21:04:01.733: E/AndroidRuntime(490): FATAL EXCEPTION: main 
02-08 21:04:01.733: E/AndroidRuntime(490): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.callmanager/com.example.callmanager.Form}: java.lang.NullPointerException 
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
02-08 21:04:01.733: E/AndroidRuntime(490): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-08 21:04:01.733: E/AndroidRuntime(490): at android.os.Looper.loop(Looper.java:123) 
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.main(ActivityThread.java:3683) 
02-08 21:04:01.733: E/AndroidRuntime(490): at java.lang.reflect.Method.invokeNative(Native Method) 
02-08 21:04:01.733: E/AndroidRuntime(490): at java.lang.reflect.Method.invoke(Method.java:507) 
02-08 21:04:01.733: E/AndroidRuntime(490): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-08 21:04:01.733: E/AndroidRuntime(490): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-08 21:04:01.733: E/AndroidRuntime(490): at dalvik.system.NativeStart.main(Native Method) 
02-08 21:04:01.733: E/AndroidRuntime(490): Caused by: java.lang.NullPointerException 
02-08 21:04:01.733: E/AndroidRuntime(490): at com.example.esoftcallmanager.Form.onCreate(Form.java:24) 
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-08 21:04:01.733: E/AndroidRuntime(490): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
02-08 21:04:01.733: E/AndroidRuntime(490): ... 11 more 

我加入下面是否有幫助

enter image description here

爲什麼我收到此錯誤的文件夾結構?如何解決這個問題?請幫忙!

+4

'這裏是我有的所有代碼,問題在哪裏'很不好尋求幫助...... – WarrenFaith 2013-02-08 15:51:10

+0

您仍然錯過了在代碼中引用NullPointer的地方標記行的部分。簡而言之:你張貼的方式超過需要,仍然錯過了重要的東西。 – WarrenFaith 2013-02-08 16:58:14

回答

0

我設法通過我自己來解決這個問題。以下編輯是必需的。

activity_form.xml

的的ListView的 「ID」 應該是

android:id="@android:id/list" 

Form.java

應該調用這個方法

ListView lv = (ListView) findViewById(android.R.id.list); 
ListView

裏面getView(),在TextViewImageView的初始化應該是這樣的

TextView tv = (TextView) view.findViewById(R.id.listText); 
ImageView iv = (ImageView) view.findViewById(R.id.listImage); 

注意view.findViewById()部分。

+1

那麼,雖然這可能會起作用,但它確實不是解決方案。你,絕對不應該把你的代碼放到android:命名空間中。相反,您應該從所有資源ID的名稱中刪除「android」前綴:R.id.foo,而不是android.R.id.foo。一般來說,你的程序應該在不導入「android.R」的情況下編譯。 – 2013-02-08 18:46:52

2

變化:

ListView lv = (ListView)findViewById(android.R.id.list); 

ListView lv = (ListView)findViewById(R.id.list); 

除非你有在的android:id="@android:id/myID"格式的XML一個id,你並不需要使用android.R。每當你使用自己的ID之一,你可以簡單地使用R.id.Whatever

+0

試過。仍然有問題 – 2013-02-08 17:38:38

+0

謝謝:)我設法解決我的問題。我回答了同樣的問題,包括我的解決方案。謝謝你幫助我:) +1從我:) – 2013-02-08 18:39:31

3

試試這個:

改變這種

ListView lv = (ListView)findViewById(android.R.id.list); 

爲:

ListView lv = (ListView)findViewById(R.id.list); 

通常情況下,他們使用android.R。 id.list當類擴展ListActivity時

+0

試過。仍然存在的問題 – 2013-02-08 17:18:48

+0

嘗試反向延伸到一個ListActivity而不是活動 – 2013-02-08 18:01:59

+1

謝謝:)我設法解決我的問題。我回答了同樣的問題,包括我的解決方案。感謝您幫助我:) +1從我:) – 2013-02-08 18:18:18

0

試試這個。

我希望它能解決你的NPE。

更改它

ListView lv = (ListView)findViewById(android.R.id.list); 

ListView lv = (ListView)findViewById(R.id.list); 

其實您已經定義佈局activity_form.xml列表視圖有名稱list,所以你需要編寫R.id.list

1

問題就在這裏。ListView lv = (ListView)findViewById(android.R.id.list);您參考Id android.R.id.list 但是,在您的佈局中,您引用了android:id="@+id/list",它在Resources類中稱爲列表中創建了一個Id。所以你應該參考R.id.list如果你調試,你會發現lv變量爲空,因爲它沒有被發現,因爲你引用了錯誤的ID。

+0

試過。仍然有問題 – 2013-02-08 17:59:00

+0

是的,但是在這個改變之後,你得到了一個不同的NPE,你注意到瞭如何解決你的答案。 – Eluvatar 2013-02-12 21:53:38

0

如果您正在使用自己的佈局,你不需要任何更多的寫android.R只是刪除Android和重組類