0

I am trying to show a simple listView with an image, a large text and a small text in each list item. I have coded the following but when I run the app, it gives a NullPointerException. For the method i have used, I do not need to findviewbyid so I'm not sure what I am doing to give this error. Anyone know?

This is my MenuPage.java file

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

public class MenuPage extends ListActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    String[]values=new String[]{"Coffee","Steak","Ice Cream"};
    String[]prices=new String[]{"$4.99","$21.99","$12.99"};
    setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.values, values));
    setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.prices, prices));


}

}

this is my menu.xml for the java file above:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
    android:id="@+id/ic_launcher_movies"
    android:layout_width="50px"
    android:layout_height="50px"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="10px"
    android:layout_marginTop="2dp"
    android:src="@drawable/items"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true">
</ImageView>
<TextView
    android:id="@+id/values"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@+id/values"
    android:textSize="25sp"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="64dp"
    android:layout_alignParentTop="true"
    android:layout_marginTop="0dp"
    android:minHeight="120dp">
</TextView>
<TextView
    android:id="@+id/prices"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@+id/prices"
    android:textSize="10sp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="120dp"
    android:layout_marginLeft="65dp">
</TextView>

also, I created an xml with the drawables for the images i want to use;

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:duration="30"
    android:drawable="@drawable/coffee"/>
<item
    android:duration="30"
    android:drawable="@drawable/steak"/>
<item
    android:duration="30"
    android:drawable="@drawable/icecream"/>

and my logcat gives this error enter image description here

any help would be appreciated! thank you

Aria
  • 389
  • 3
  • 7
  • 25
  • *this is my `menu.xml` for the java file above:* + *`new ArrayAdapter(this,R.layout.activity_main` ...* = learn android's basics – Selvin Aug 27 '15 at 19:25
  • i know the basics. That was just a mistake. thanks – Aria Aug 27 '15 at 19:37
  • this is incorrect: setListAdapter(new ArrayAdapter(this,R.layout.activity_main,R.id.values, values)); Take a look at this: http://www.vogella.com/tutorials/AndroidListView/article.html Particularly to this: final StableArrayAdapter adapter = new StableArrayAdapter(this, android.R.layout.simple_list_item_1, list); listview.setAdapter(adapter); The "android.R.layout.simple_list_1, list" is of your interest. – Nactus Aug 27 '15 at 20:03
  • also, see the error you're getting in the console: "attempt to invoke virtual method <...setText> on a null object ... that means it can't work with your current layout (or it can't "set the text from your source" into your current layout. Furthermore, look at the ArrayAdapter documentation: http://developer.android.com/reference/android/widget/ArrayAdapter.html – Nactus Aug 27 '15 at 20:16

1 Answers1

0

I think the problem in the menu layout file

android:text="@+id/values"

try to write some text or specify link to string resource like

android:text="@string/my_button_text"

And when you call setListAdpate two times you ovirwrite first adapter with new. Have a look to example here ListActivity

w33
  • 36
  • 3
  • I'm sorry, it didn't really explain how i could relate it to mine. would you be able to explain it further? how do I merge both setListAdapters? – Aria Aug 27 '15 at 19:53
  • You cannot merge both. You need to make you own implemntation of ArrayAdapter. You can have a look here [http://stackoverflow.com/questions/2265661/how-to-use-arrayadaptermyclass](http://stackoverflow.com/questions/2265661/how-to-use-arrayadaptermyclass) – w33 Aug 28 '15 at 05:31