首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在OnItemSelectedListener上实现ListView

在OnItemSelectedListener上实现ListView
EN

Stack Overflow用户
提问于 2014-03-27 09:44:05
回答 2查看 595关注 0票数 0

嘿,我正在尝试让Listener工作,应用程序运行,一切都很好,但是当我在ListView中选择一个项目时,我会得到一个Null pointer exception错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{code.me/code.me.QuestionActivity}: java.lang.NullPointerException

代码语言:javascript
复制
public class ItemListFragment extends android.app.ListFragment 
{
    ListView lv;
    Context c;
    List<Question> questions= new ArrayList<Question>();
    Integer[] Q_Id ;
    String[] AskerUserName =   {"Jack"      ,"John"    ,"Lio"      ,"Sam"         ,"Mike"        };
    String[] AnswererUserName ={"Jacob"     ,"Mario"   ,"Tom"      ,"Shon"        ,"Jasmine"     };
    String[] Qusetion =        {"What?"     ,"Where?"  ,"When"     ,"How?"        ,"Who?"        };
    String[] Answer =          {"jjjjjjjjjj","llllllll","fffffffff","eeeeeeeeeeee","oooooooooooo"};

    @Override
    public View onCreateView( LayoutInflater inflater, 
        ViewGroup container,
        Bundle savedInstanceState )
    {


        View view = inflater.inflate(R.layout.frag1, container, false);


        lv = (ListView) view.findViewById(android.R.id.list);

        for (int i = 0;i<4;i++){
            int qid =  i;
            String AUName = AskerUserName[i].toString();
            String AnUName = AnswererUserName[i].toString();
            String Ans = Answer[i].toString();
            String Ques = Qusetion[i].toString();
            Question question = new Question(qid, AUName, AnUName, Ans, Ques);
            questions.add(question);




        }
        QuestionAdapter QuestAd = new QuestionAdapter(getActivity(),questions);
        lv.setAdapter(QuestAd);

        QuestAd.notifyDataSetChanged();




        return view;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        Intent i = new Intent(getActivity(),QuestionActivity.class);
        i.putStringArrayListExtra("answers", questions.get(position).getAnswers());
        i.putExtra("question", questions.get(position).getQuestion().toString());
        startActivity(i);
        super.onListItemClick(l, v, position, id);  

    }






        // TODO Auto-generated method stub







}

QuestionActivity

代码语言:javascript
复制
public class QuestionActivity extends Activity {
    String Question = "";
    List<String> Answers = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.question_activity);

        Intent i = new Intent(); //This should be getIntent();

        Answers.addAll(i.getStringArrayListExtra("answers"));

        Question = i.getSerializableExtra("question").toString();

        TextView question = (TextView) findViewById(id.questiontxt);
        question.setText(Question);




    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        return super.onCreateOptionsMenu(menu);
    }



}

清单

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="code.me"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" android:allowTaskReparenting="false" android:debuggable="true">
        <activity
            android:name="code.me.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

       <activity android:name=".QuestionActivity"/>




    </application>

</manifest>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-27 09:45:48

您可以摆脱lv.setOnItemSelectedListener,因为您正在扩展ListFragment,所以您应该重写onListItemClick

从医生那里:

代码语言:javascript
复制
This method will be called when an item in the list is selected. Subclasses should override.  

这里您可以找到文档

票数 1
EN

Stack Overflow用户

发布于 2014-03-27 09:53:09

在这里,您的类是用onListItemClick扩展的,因此您需要用OnItemSelectedListener来覆盖ListView,而不是ListView。

有关更多细节,请访问这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22683659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档