-2

Can anyone please tell me how to fix the below code? I am trying to setonclick listner and its causing null exception

ArrayList<Contact> imageArry = new ArrayList<Contact>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

adapter = new ContactImageAdapter1(this, R.layout.screen_list1,
imageArry);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {         
       public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                // Send intent to SingleViewActivity
           Intent i = new Intent(MainActivity.this, SingleViewActivity.class);
           // Pass image index
           i.putExtra("id", position);
           startActivity(i);
           } });

Edit

--

When I click on the image its not sending me to another activity

this my activity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="10dip" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:text="Latest" />

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"

            android:background="#B29090" >
        </ListView>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:gravity="center_vertical"
            android:text="Recomanded" />

        <ListView
            android:id="@+id/list1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:background="#4A9C67" >
        </ListView>
    </LinearLayout>

</ScrollView>
josedlujan
  • 5,357
  • 2
  • 27
  • 49
Moudiz
  • 7,211
  • 22
  • 78
  • 156

4 Answers4

1

Your variable listView where you want to add a listener seems not initialized

Climbatize
  • 1,103
  • 19
  • 34
  • Not necessarily.. It's likely he instantiated as an instance variable – adao7000 Jun 08 '15 at 15:28
  • yes you are right i didnt notice that .. the app now is working but why when i click on the photo its not sending me to another activity, why you think? – Moudiz Jun 08 '15 at 15:31
1

Your listView is dataList not listView

You should write :

dataList.setOnItemClickListener(...)
MatthieuLemoine
  • 718
  • 6
  • 11
1

Set onClickListener() to your ListView variable datalist. You're currently setting it to listView which exists no where else in your code.

ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
dataList.setOnItemClickListener(new OnItemClickListener() {...}       
Bidhan
  • 10,607
  • 3
  • 39
  • 50
  • yes you are bidhan I didnt notice it , by the way as i mentioned is the previous comment I cant click on the image – Moudiz Jun 08 '15 at 15:32
0

Change your code to this :

ArrayList<Contact> imageArry = new ArrayList<Contact>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

adapter = new ContactImageAdapter1(this, R.layout.screen_list1,
imageArry);
ListView dataList = (ListView) findViewById(R.id.list);
dataList.setAdapter(adapter);
dataList .setOnItemClickListener(new OnItemClickListener() {         
       public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                // Send intent to SingleViewActivity
           Intent i = new Intent(MainActivity.this, SingleViewActivity.class);
           // Pass image index
           i.putExtra("id", position);
           startActivity(i);
           } });

Your code were fine, the problem what you have faced is this listview is NULL because listview it's not declared, I guess you misstyped the ListView, you only have to change listview to dataList.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • yes tnx u but now i am not able to click on the photo , can you know why? i edit my question – Moudiz Jun 08 '15 at 15:38
  • there is no error i can open the app .. but the pothos in the list are not clickable ,, I mean if i click on a photo its not sending me to the another activity .. Its problem with my xml.. because if i remove the setclicklistner I am still not able to click the photo – Moudiz Jun 08 '15 at 15:41
  • Well, maybe you can create another post, the main question of this thread it's already answered, it is not? I think you should create another post asking this new problem that you've faced. You can post in your post or on this comment a link of your new answer, and I'll take a look ;) – Skizo-ozᴉʞS ツ Jun 08 '15 at 15:52
  • yes I did post a new question you can check if you can help me plz – Moudiz Jun 08 '15 at 16:12