2011-12-17 40 views
1

它之前工作,但由於未知的原因,即使一切正常,我仍然有空指針異常。看到自己,如果你能幫助:爲什麼我無法使用getView()中的LayoutInflater讓BaseAdapter工作?它始終返回空指針異常

public class TestActivity extends ListActivity { 

ArrayList<String> array; 

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

    setContentView(R.layout.main); 
    array = getsms(); 
    SMSadapter adapter = new SMSadapter(this, array); 
    getListView().setAdapter(adapter); 

    } 

public class SMSadapter extends BaseAdapter { 

    Context mcontext; 
    ArrayList<String> marray; 
    LayoutInflater inflater; 

    public SMSadapter (Context context, ArrayList<String>array){ 

     mcontext = context; 
     marray = array; 
     inflater = LayoutInflater.from(context); 

    } 

    @Override 
    public int getCount() { 
     return marray.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return marray.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    public class ViewHolder { 
     TextView text; 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = new ViewHolder(); 
     if (convertView == null){ 
      convertView = inflater.inflate(R.layout.items, null); 
      holder.text = (TextView)findViewById(R.id.text); 
      convertView.setTag(holder); 
     }else{ 
      holder = (ViewHolder)convertView.getTag(); 
     } 

     holder.text.setText(marray.get(position)); 
     return convertView; 
    } 

} 

public ArrayList<String> getsms(){ 
    Cursor cursor = getContentResolver().query(Uri.parse("content://sms/inbox"), new String[]{"address"}, null,null,null); 
    cursor.moveToFirst(); 
    ArrayList<String> temp = new ArrayList<String>(); 
    while(cursor.moveToNext()){ 
     temp.add(cursor.getString(0)); 
    } 
    return temp; 

} 

}

12-17 16:11:17.037: E/AndroidRuntime(558): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 
12-17 16:11:17.037: E/AndroidRuntime(558): at android.view.View.measure(View.java:8313) 
12-17 16:11:17.037: E/AndroidRuntime(558): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531) 
12-17 16:11:17.037: E/AndroidRuntime(558): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309) 
12-17 16:11:17.037: E/AndroidRuntime(558): at android.view.View.measure(View.java:8313) 
12-17 16:11:17.037: E/AndroidRuntime(558): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 
12-17 16:11:17.037: E/AndroidRuntime(558): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 
12-17 16:11:17.037: E/AndroidRuntime(558): at android.view.View.measure(View.java:8313) 
12-17 16:11:17.037: E/AndroidRuntime(558): at android.view.ViewRoot.performTraversals(ViewRoot.java:839) 
12-17 16:11:17.037: E/AndroidRuntime(558): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859) 
12-17 16:11:17.037: E/AndroidRuntime(558): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-17 16:11:17.037: E/AndroidRuntime(558): at android.os.Looper.loop(Looper.java:123) 
12-17 16:11:17.037: E/AndroidRuntime(558): at android.app.ActivityThread.main(ActivityThread.java:3683) 
12-17 16:11:17.037: E/AndroidRuntime(558): at java.lang.reflect.Method.invokeNative(Native Method) 
12-17 16:11:17.037: E/AndroidRuntime(558): at java.lang.reflect.Method.invoke(Method.java:507) 
12-17 16:11:17.037: E/AndroidRuntime(558): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
12-17 16:11:17.037: E/AndroidRuntime(558): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
12-17 16:11:17.037: E/AndroidRuntime(558): at dalvik.system.NativeStart.main(Native Method) 
12-17 16:22:06.778: D/AndroidRuntime(587): Shutting down VM 
12-17 16:22:06.778: W/dalvikvm(587): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
12-17 16:22:06.788: E/AndroidRuntime(587): FATAL EXCEPTION: main 
12-17 16:22:06.788: E/AndroidRuntime(587): java.lang.NullPointerException 
12-17 16:22:06.788: E/AndroidRuntime(587): at com.test.TestActivity$SMSadapter.getView(TestActivity.java:74) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.AbsListView.obtainView(AbsListView.java:1430) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.ListView.onMeasure(ListView.java:1127) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.View.measure(View.java:8313) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.View.measure(View.java:8313) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.View.measure(View.java:8313) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.View.measure(View.java:8313) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.View.measure(View.java:8313) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.ViewRoot.performTraversals(ViewRoot.java:839) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.os.Handler.dispatchMessage(Handler.java:99) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.os.Looper.loop(Looper.java:123) 
12-17 16:22:06.788: E/AndroidRuntime(587): at android.app.ActivityThread.main(ActivityThread.java:3683) 
12-17 16:22:06.788: E/AndroidRuntime(587): at java.lang.reflect.Method.invokeNative(Native Method) 
12-17 16:22:06.788: E/AndroidRuntime(587): at java.lang.reflect.Method.invoke(Method.java:507) 
12-17 16:22:06.788: E/AndroidRuntime(587): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
12-17 16:22:06.788: E/AndroidRuntime(587): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
12-17 16:22:06.788: E/AndroidRuntime(587): at dalvik.system.NativeStart.main(Native Method) 

回答

1

你有這樣的錯誤:

holder.text = (TextView)findViewById(R.id.text); 

相反,你必須這樣做:

holder.text = (TextView)convertView.findViewById(R.id.text); 

希望這有助於!

+0

對你有幫助嗎? – 2011-12-17 16:55:34

+0

是的,它的工作。非常感謝,我完全忘記了這樣做。 – Samet 2011-12-17 16:59:54