-1

我正在使用自定义适配器来显示列表视图。但是我得到了 NullPointerException。

    public class moderationrequests extends Fragment {
    public moderationrequests() {
    }

    private moderationrequestsadapter tableRowAdapter;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v("ListFragment", "onCreate()");
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.v("ListFragment", "onActivityCreated().");
        Log.v("ListsavedInstanceState", savedInstanceState == null ? "true"
                : "false");

        // Generate list View from ArrayList
        displayListView();

    }

    public static final String ARG_SECTION_NUMBER = "section_number";

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater
                .inflate(R.layout.moderationrequests, container, false);
        return v;
    }

    private void displayListView() {

        String[] urlList = { "From Krishna on Business - 2 hours ago.",
                "From Krishna on Business - 2 hours ago." };


        tableRowAdapter = new moderationrequestsadapter(getActivity(),
                R.layout.mod_req_list, urlList);
        ListView listView = (ListView) getView().findViewById(R.id.list_view);
        listView.setAdapter(tableRowAdapter);


    }
}

适配器类

   public class moderationrequestsadapter extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] names;
    private Button deleteButton = null;

    public moderationrequestsadapter(Activity context, int itemResId,
            String[] names) {
        super(context, itemResId, names);
        this.context = context;
        this.names = names;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // LayoutInflater inflater = context.getLayoutInflater();
        // View rowView = inflater.inflate(R.layout.mod_req_list, null, true);

        View rowView = null;
        if (convertView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.mod_req_list, null);

        } else {
            rowView = convertView;
        }

        deleteButton = (Button) rowView.findViewById(R.id.icon);
        deleteButton.setTag(position);
        TextView textView = (TextView) rowView.findViewById(R.id.rowId);
        textView.setText(names[position]);
        Log.d("views", rowView.toString());
        return rowView;

    }

}

日志猫

07-10 09:10:38.280: E/AndroidRuntime(3113): FATAL EXCEPTION: main
07-10 09:10:38.280: E/AndroidRuntime(3113): java.lang.NullPointerException
07-10 09:10:38.280: E/AndroidRuntime(3113):     at com.main.udebate.moderationrequestsadapter.getView(moderationrequestsadapter.java:40)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.AbsListView.obtainView(AbsListView.java:2159)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.ListView.onMeasure(ListView.java:1158)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:666)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1389)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.View.measure(View.java:15518)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.Choreographer.doFrame(Choreographer.java:532)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.os.Handler.handleCallback(Handler.java:725)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.os.Looper.loop(Looper.java:137)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at java.lang.reflect.Method.invokeNative(Native Method)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at java.lang.reflect.Method.invoke(Method.java:511)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-10 09:10:38.280: E/AndroidRuntime(3113):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

您使用的适配器错误。它是一个ArrayAdapter<String>,意味着它已经有一个ArrayList<String>来保存数据。无需声明额外的字符串数组。只需将names数据添加到适配器:

myAdapter.addAll(names);

创建适配器后。

内部getView()获取数据项为:

String name = getItem(position);
于 2013-07-10T09:37:58.777 回答
0

尝试在下面的行中初始化适配器时getActivity().getApplicationContext()代替getActivity()

new moderationrequestsadapter(getActivity(), R.layout.mod_req_list, urlList);

并在适配器的构造函数中接受它时,将其更改为 Context 而不是 Activity。

此外,在适配器中膨胀视图时,请在适配器的 getView 方法中尝试以下代码段:

View rowView = convertView;
if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.mod_req_list, null);

} 
    deleteButton = (Button) rowView.findViewById(R.id.icon);
    deleteButton.setTag(position);
    TextView textView = (TextView) rowView.findViewById(R.id.rowId);
    textView.setText(names[position]);
    Log.d("views", rowView.toString());
    return rowView;
于 2013-07-10T10:09:13.537 回答