0

嘿,我試圖讓Listener工作,在應用程序運行,一切都很好,但是當我選擇在ListView一個項目,我得到一個錯誤Null pointer exception實施OnItemSelectedListener在一個ListView

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

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

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); 
    } 



} 

清單

<?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> 
+0

使用'OnItemClickListener' –

回答

1

你可以擺脫lv.setOnItemSelectedListener,因爲要擴展ListFragment你應該重寫onListItemClick

從DOC:

This method will be called when an item in the list is selected. Subclasses should override. 

here你可以找到的文檔

+0

謝謝!另一件事;關於意圖任何想法清單應該是什麼樣子,知道有一個MainActivity有兩個碎片,這是一個 – SaleemKhair

+0

@SaleemKhair'Manifest'不關心碎片的數量你'活動'有:) – Droidman

+0

作爲@Droidman正確地說,清單不關心'片段' – Blackbelt

0

這裏你的類是ListFragment延伸,所以你需要用onListItemClick,而不是OnItemSelectedListener爲ListView的覆蓋。

有關詳情,請Here