setAdapter方法中的NullPointerException

时间:2014-10-14 09:18:52

标签: java android nullpointerexception

我有一个CustomListView适配器,有一个图像视图和两个textview。我从数据库中获取数据并将其显示在列表视图中。有两个textview,一个是手机号码,另一个是用户名。目前我只想显示手机号码和名称。我使用我的过滤器实现了搜索功能,但是当我运行我的项目时,它在setAdapter方法中给了我NullPointerException。

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

    <EditText
        android:id="@+id/search_text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingTop="50dp"
        android:layout_below="@+id/listview" />

</RelativeLayout>

ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter {
Context mContext;
LayoutInflater inflater;
private List<UserDetailFilter> worldpopulationlist = null;
private ArrayList<UserDetailFilter> arraylist;

public ListViewAdapter(Context context, List<UserDetailFilter> worldpopulationlist) {
    mContext = context;
    this.worldpopulationlist = worldpopulationlist;
    inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.arraylist = new ArrayList<UserDetailFilter>();
    this.arraylist.addAll(worldpopulationlist);
}

public class ViewHolder {
    TextView username;
    TextView mobilenumber;
}

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

@Override
public UserDetailFilter getItem(int position) {
    return worldpopulationlist.get(position);
}

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

public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.listview_layout, null);
        holder.username = (TextView) view.findViewById(R.id.user_name);
        holder.mobilenumber = (TextView) view.findViewById(R.id.user_mobile);
        //holder.imageView = (ImageView) view.findViewById(R.id.imageView);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    holder.username.setText(worldpopulationlist.get(position).getCountry());
    holder.mobilenumber.setText(worldpopulationlist.get(position).getPopulation());
    return view;
}

// Filter Class
public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    worldpopulationlist.clear();
    if (charText.length() == 0) {
        worldpopulationlist.addAll(arraylist);
    }
    else
    {
        for (UserDetailFilter wp : arraylist)
        {
            if (wp.getCountry().toLowerCase(Locale.getDefault()).contains(charText))
            {
                worldpopulationlist.add(wp);
            }
        }
    }
    notifyDataSetChanged();
}

}

UserDetailFilter.java

public class UserDetailFilter {
private String username;
private String mobilenumber;

public UserDetailFilter(String username, String mobilenumber) {
    this.username = username;
    this.mobilenumber = mobilenumber;
}

//public String getRank() {
//  return this.rank;
//}

public String getCountry() {
    return this.username;
}

public String getPopulation() {
    return this.mobilenumber;
}

}

现在当我打电话

listViewAdapter = new ListViewAdapter(getBaseContext(), arraylist);
listView.setAdapter(listViewAdapter);

给我nullpoinerexception

 10-14 14:31:48.943    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕     java.lang.NullPointerException
 10-14 14:31:48.953    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕ at com.example.jamesbond.ahircommunity.UserDetail.onCreate(UserDetail.java:70)
 10-14 14:31:48.963    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5008)
 10-14 14:31:48.973    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
 10-14 14:31:48.973    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
 10-14 14:31:48.983    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
 10-14 14:31:48.993    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕ at android.app.ActivityThread.access$600(ActivityThread.java:130)
 10-14 14:31:49.003    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
 10-14 14:31:49.003    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:99)
 10-14 14:31:49.013    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
 10-14 14:31:49.013    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:4745)
 10-14 14:31:49.023    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
 10-14 14:31:49.033    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:511)
 10-14 14:31:49.033    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
 10-14 14:31:49.043    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 10-14 14:31:49.053    2544-2544/com.example.jamesbond.ahircommunity W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

UserDetail.java

    package com.example.jamesbond.ahircommunity;
import android.os.StrictMode;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.ListView;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Locale;

public class UserDetail extends ActionBarActivity {
    ListView listView;
    ListViewAdapter listViewAdapter;
    EditText editsearch;
    String[] nameOfPerson;
    String[] mobileNumber;
    ArrayList<UserDetailFilter> arraylist = new ArrayList<UserDetailFilter>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_layout);

        /**
         * POST PARAMETER MUST INCLUDE HERE
         */
        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        String response;
        int SDK_INT = android.os.Build.VERSION.SDK_INT;
        if (SDK_INT > 8) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        try {
            response = CustomHttpClient
                    .executeHttpPost(
                            "myurl",
                            postParameters);

            Log.d("JSON:",response);
            response.toString();
            JSONArray jsonArray = new JSONArray(response);
            int length = jsonArray.length();
            System.out.println(length);
            nameOfPerson = new String[10];
            mobileNumber = new String[10];
            for (int i = 0; i < 10; i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                nameOfPerson[i] = jsonObject.getString("firstname");
                mobileNumber[i] = jsonObject.getString("mobile");
                Log.d(nameOfPerson[i], mobileNumber[i]);
            }
            listView = (ListView) findViewById(R.id.listview);
            for (int i = 0; i < 10; i++)
            {
                UserDetailFilter wp = new UserDetailFilter(nameOfPerson[i],
                        mobileNumber[i]);
                arraylist.add(wp);
            }

            listViewAdapter = new ListViewAdapter(getBaseContext(), arraylist);
            listView.setAdapter(listViewAdapter);
            editsearch = (EditText) findViewById(R.id.search_text);
            editsearch.addTextChangedListener(new TextWatcher()
            {

                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                    String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
                    listViewAdapter.filter(text);
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                                              int arg2, int arg3) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
                    // TODO Auto-generated method stub
                }
            });

        } catch (Exception e) {

            e.printStackTrace();

        }

    }
    /*
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem searchViewMenuItem = menu.findItem(R.id.);
        SearchView mSearchView = (SearchView) searchViewMenuItem.getActionView();
        int searchImgId = getResources().getIdentifier("android:id/search_button", null, null);
        ImageView v = (ImageView) mSearchView.findViewById(searchImgId);
        v.setImageResource(R.drawable.google_web_search);
        mSearchView.setOnQueryTextListener(this);
        return super.onPrepareOptionsMenu(menu);
    }
    */
}

任何人都可以告诉我这段代码我做错了什么。请帮助我,我正在尝试所有我知道的方式但我无法弄清楚这段代码中的错误。

0 个答案:

没有答案