2013-07-27 111 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

非常感谢:) –