2013-07-27 150 views
0

我是非常新的Android編程。我試圖創建一個基本的列表app.I geting nullpointer異常,同時使用setAdapter()設置適配器。我的代碼是相同的如下。Android:獲取空指針異常,而setAdapter()

ListAdapter.java

package com.example.listexmpl; 
import java.util.ArrayList; 

import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

public class ListAdapter extends BaseAdapter{ 

    private ArrayList<ListRecord> records = new ArrayList<ListRecord>(); 


    public ListAdapter(){ 
     records.add(new ListRecord("Rajat","I'm feeling very happy today.")); 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return records.size(); 
    } 

    @Override 
    public Object getItem(int index) { 
     // TODO Auto-generated method stub 
     return getItem(index); 
    } 

    @Override 
    public long getItemId(int index) { 
     // TODO Auto-generated method stub 
     return index; 
    } 

    @Override 
    public View getView(int index, View view, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     if(view==null){ 
      LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 
      view = inflater.inflate(R.layout.list_record,parent,false); 
     } 
     ListRecord listr = records.get(index); 
     TextView nameView = (TextView) view.findViewById(R.id.name_view); 
     TextView statView = (TextView) view.findViewById(R.id.stat_view); 
     nameView.setText(listr.getName()); 
     statView.setText(listr.getStatus()); 
     return view; 
    } 

} 

ListActivity.java

package com.example.listexmpl; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.ListView; 

public class ListActivity extends Activity { 

    ListAdapter lstrecordAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_list); 
     ListView lstview = null; 
     lstview = (ListView) findViewById(R.id.list_record); 
     lstrecordAdapter = new ListAdapter(); 
     lstview.setAdapter(lstrecordAdapter); 
    } 

    @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_list, menu); 
     return true; 
    } 

} 

當我把包含的try-catch setAdapter()阻止應用程序運行的發言,但沒有什麼除了應用程序標題之外,請在屏幕上查看。我在哪裏出錯了?

[編輯] activity_list.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 

</ListView> 
+0

post activity_List – Blackbelt

+0

@blackbelt:完成 –

回答

3

你錯過了ID項爲您的ListView

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/listviewid" 
    > 

</ListView> 

裏面的活性變化

ListView lstview = null; 
lstview = (ListView) findViewById(R.id.list_record); 

ListView lstview = null; 
lstview = (ListView) findViewById(R.id.listviewid); 
+1

非常感謝:) –