0

hey there I'm trying to get the Listener to work, the application runs and all is fine but when i select an item in the ListView I get a Null pointer exception error

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



}

MANIFEST

<?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>
SaleemKhair
  • 499
  • 3
  • 12

2 Answers2

1

you can get rid of lv.setOnItemSelectedListener, since you are extending ListFragment you should override onListItemClick .

From the doc:

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

here you can find the documentation

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Thank you! that worked, One more thing ;about the intent any Idea what the Manifest should look like , knowing that there is one MainActivity with two frags and this is one – SaleemKhair Mar 27 '14 at 09:55
  • @SaleemKhair the `Manifest` doesn't care about the number of `Fragments` you `Activity` has :) – Droidman Mar 27 '14 at 10:00
  • as @Droidman correctly said, the Manifest does not care of `Fragment` – Blackbelt Mar 27 '14 at 10:07
  • @Droidman I think you got my question wrong , What i meant to ask is that; this `intent` in the `Fragment` that is starting an `Activity`; how can i properly define the new `Activity` in my `Manifest` – SaleemKhair Mar 27 '14 at 10:33
  • @SaleemKhair take a look [here](http://stackoverflow.com/questions/15699192/have-you-declared-this-activity-in-your-androidmanifest-xml) – Blackbelt Mar 27 '14 at 10:40
  • @blackbelt check out the `Manifest` i have just added it to my question – SaleemKhair Mar 27 '14 at 10:53
  • `java.lang.RuntimeException: Unable to start activity ComponentInfo{code.me/code.me.QuestionActivity}: java.lang.NullPointerException` thanks for your time @blackbelt i think `ComponentInfo` has to do with what I'm sending through my `Intent` or something – SaleemKhair Mar 27 '14 at 10:59
  • Question has been Updated @blackbelt – SaleemKhair Mar 27 '14 at 11:35
  • the problem was missing the line `i = getIntent();` – SaleemKhair Mar 27 '14 at 11:46
  • So you figured it out by yourself? – Blackbelt Mar 27 '14 at 11:49
0

Here your class is extends with ListFragment so you need to override with onListItemClick instead of OnItemSelectedListener for ListView.

For more details go Here

Piyush
  • 18,895
  • 5
  • 32
  • 63