在数组适配器类中设置TextView的值时,出现空指针异常-Android [英] Null Pointer Exception while setting value of TextView in an Array Adapter class - Android

查看:99
本文介绍了在数组适配器类中设置TextView的值时,出现空指针异常-Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试启动包含ListView的活动时得到了NullPointerException.

I'm getting a NullPointerException while trying to start an Activity which contains a ListView .

在适配器类的getView方法中,当调用textViewsetText函数时会发生异常.

In the getView method of the adapter class, the exception happens when the setText function of a textView is being called .

下面的代码是我的适配器类:

The code bellow is my adapter class:

public class QuestionsListAdapter extends ArrayAdapter<Question> {

    Context context;
    List<Question> questions;

    public QuestionsListAdapter(Context context, List<Question> questions){

        super(context, R.layout.list_item_question, questions);
        this.context = context;
        this.questions = questions;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = vi.inflate(R.layout.list_item_question, null);

        Question question = questions.get(position);

        TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);

        Log.i(TableCreator.LOG_TAG, question.toString()); //this works fine and the string is not null .

        tv.setText(question.toString()+""); //NULL POINTER EXCEPTION .

        return view;
    }
}

如您所见,我已将字符串记录在logcat中,并且工作正常,但下一行会出错. 这是logcat的输出:

As you see, I've logged the string in the logcat and it works just fine, but the next line makes the mistake . And this is the logcat output:

05-27 13:24:02.979    5325-5325/org.kabiri.operationcheklist I/Operation Checklist﹕ |-Question-> id:1 summary:mySummary comment:myComment solution:mySolution ownerList:dummyOwner
05-27 13:24:02.979    5325-5325/org.kabiri.operationcheklist D/AndroidRuntime﹕ Shutting down VM
05-27 13:24:02.979    5325-5325/org.kabiri.operationcheklist W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0f5f648)
05-27 13:24:02.979    5325-5325/org.kabiri.operationcheklist E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at org.kabiri.operationchecklist.adapter.QuestionsListAdapter.getView(QuestionsListAdapter.java:43)
            at android.widget.AbsListView.obtainView(AbsListView.java:2177)
            at android.widget.ListView.makeAndAddView(ListView.java:1840)
            at android.widget.ListView.fillDown(ListView.java:675)
            at android.widget.ListView.fillFromTop(ListView.java:736)
            at android.widget.ListView.layoutChildren(ListView.java:1655)
            at android.widget.AbsListView.onLayout(AbsListView.java:2012)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:502)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
            at android.view.View.layout(View.java:14289)
            at android.view.ViewGroup.layout(ViewGroup.java:4562)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
            at android.view.Choreographer.doCallbacks(Choreographer.java:562)
            at android.view.Choreographer.doFrame(Choreographer.java:532)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

logcat显示错误发生在我的适配器类的这一行:

The logcat shows that the error happens on this line of my adapter class:

tv.setText(question.toString()+""); 

非常感谢您的帮助.

推荐答案

您已经知道问题出在哪里!

You already know where the problem is!

tv.setText(question.toString()+"");

导致NPE,这意味着TextView tvnull.那意味着那行

is causing the NPE that means the TextView tv is null. And that means that the line

 TextView tv = (TextView) view.findViewById(R.id.question_list_item_string);

无法找到TextView.检查question_list_item_string ID,并确保它与您的list_item_question.xml文件中的ID匹配

is not able to find the TextView. Check the question_list_item_string id and make sure it matches the id in your list_item_question.xml file

这篇关于在数组适配器类中设置TextView的值时,出现空指针异常-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆