MyCustomAdapter.getView中的java.lang.NullPointerException

时间:2012-10-14 08:14:25

标签: android android-layout nullpointerexception contacts

我想创建一个多联系人选择器。最初我只是试图通过提供静态值来测试它是否有效。我想实现一个CustomAdapter来执行任务,但我得到java.lang.NullPointerException并且根据log cat它在第一行。 41是paramView = mInflater.inflate(R.layout.custcontactview, null);

任何人都可以告诉我我做错了什么吗?

注意:如果有人知道有关创建多个联系人选择器的好教程请提供链接。

Log Cat

10-13 19:44:38.115: E/AndroidRuntime(1461): java.lang.NullPointerException
10-13 19:44:38.115: E/AndroidRuntime(1461):     at com.example.multiplecontacts.MyCustomAdapter.getView(MyCustomAdapter.java:41)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.widget.AbsListView.obtainView(AbsListView.java:1430)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.widget.ListView.makeAndAddView(ListView.java:1745)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.widget.ListView.fillDown(ListView.java:670)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.widget.ListView.fillFromTop(ListView.java:727)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.widget.ListView.layoutChildren(ListView.java:1598)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.widget.AbsListView.onLayout(AbsListView.java:1260)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.view.View.layout(View.java:7175)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.view.View.layout(View.java:7175)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.view.View.layout(View.java:7175)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.view.View.layout(View.java:7175)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.view.View.layout(View.java:7175)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.os.Looper.loop(Looper.java:130)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at java.lang.reflect.Method.invokeNative(Native Method)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at java.lang.reflect.Method.invoke(Method.java:507)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-13 19:44:38.115: E/AndroidRuntime(1461):     at dalvik.system.NativeStart.main(Native Method)

custcontactview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5.0px"
android:paddingLeft="5.0px"
android:paddingTop="5.0px" >

<TextView
    android:id="@+id/txtContactName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15.0dip"
    android:layout_toLeftOf="@+id/checkBox1"
    android:layout_alignParentLeft="true"
    android:text="Medium Text"
    android:textAppearance="?android:textAppearanceMedium" />

<TextView
    android:id="@+id/txtContactNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txtContactName"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="15.0dip"
    android:layout_toLeftOf="@+id/checkBox1"
    android:text="Small Text"
    android:textAppearance="?android:textAppearanceSmall" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false" />

</RelativeLayout>

activity_main.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" >


<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />


</RelativeLayout>

MainActivity.java

public class MainActivity extends ListActivity {

ListView mListView;
MyCustomAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mAdapter = new MyCustomAdapter();
    mListView = getListView();
    mAdapter.addItem("Jay", "+919033953469");
    mAdapter.addItem("Jay", "+918469000500");
    mAdapter.addItem("Tapan", "+919033843969");
    mAdapter.addItem("Tapan", "+919408396544");     
    mListView.setAdapter(mAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

MyCustomAdapter.java

public class MyCustomAdapter extends BaseAdapter {


private LayoutInflater mInflater;
private ArrayList<String> mData = new ArrayList();
private ArrayList<String> mNumber = new ArrayList();

public MyCustomAdapter() {
}

public int getCount() {
    return this.mData.size();
}

public void addItem(String paramString1, String paramString2) {
  this.mData.add(paramString1);
  this.mNumber.add(paramString2);
  notifyDataSetChanged();
}

public Object getItem(int paramInt) {
    return (String) this.mData.get(paramInt);
}

public long getItemId(int paramInt) {
    return paramInt;
}

public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
    if(paramView == null) {
        paramView = mInflater.inflate(R.layout.custcontactview, null);
    }
    /*CheckBox cbx = (CheckBox) paramView.findViewById(R.id.checkBox1);
    cbx.setTag(paramInt);
    TextView tvTitle = (TextView) paramView.findViewById(R.id.txtContactName);
    TextView tvTitle1 = (TextView) paramView.findViewById(R.id.txtContactNumber);
    tvTitle.setText(mData.get(paramInt).toString());
    tvTitle1.setText(mNumber.get(paramInt).toString());*/
    return paramView;
}

public static class ViewHolder {
    public CheckBox cb;
    public TextView nameView;
    public TextView numberView;
}

}

1 个答案:

答案 0 :(得分:0)

mInflater为NULL

public MyCustomAdapter(Context context) {
    mInflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}