this is my first time using the ListView and have have tried out many ways to make this workable. Firstly I used within the xml file under the to specify the “android:entries” with a string-array, it displayed but I could not activate the onClickListener. The other method were similar to the one that I have, some were where I used “extend Acitvity” instead. Thanks in advance for helping me.
Basically, I want to use a customized textView row for each item on the list and show a toast for each clickable row. My problem is that the application would crash the moment I try to display this activity. I am not sure where the problem is as there isn’t any syntax error. The error messages is ( Sorry, I would place a picture but I can’t – new user ) :
- FATAL EXCEPTION: main
- java.lang.RuntimeException:Unable to start activity ComponentInfo(edu…..)
- at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
- at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2237)
- at android.app.ActivityThread.access$600(ActivityTHread.java:139)
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
- at android.os.Handler.dispatchMessage(Handler.java:99)
- at android.os.Looper.loop(Loooper.java:154)
- at android.app.ActivityThread.main(ActivityThread.java:4974)
- at java.lang.reflect.Method.invokeNative(Native Method)
- at java.lang.reflect.Method.invoke(Method java:511)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
- at com.android.internal.os.ZygoteInit.main(ZygotaInit.java:551)
- at dalvik.system.NativeStart.main(Native Method)
- Caused by:java.lang.UnsupportedOperationException: addView(View, LayoutParams)…
- at android.widget.AdapterView.addView(AdapterView.java:471)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:743)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:489)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:396)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:352)
- at com.android.internal.policy.impl.PhoneWindows.setContentView(PhoneWindow.java …)
- at android.app.Activity.setContentView(Activity.java:1892)
- at edu.nyp.wonderful.Menu.onCreate(Menu.java:14)
- at android.app.Activity.performCreate(Activity.java:4538)
- at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
- at android.app.ActivityThread.performLaunchActivity(ActivityTHread.java:2158)
- … 11 more lines ( I cannot show more to type out )
Menu.java v
package edu.nyp.wonderful;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Menu extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
Log.d("Menu:", "after set content view");
String[] items = { "Set Pins", "View Pins", "List of Pins", "Email Summary", "Edit Session's Info" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.menu_row, R.id.menu_name, items);
setListAdapter(adapter);
Log.d("Menu:", "after setting adapter");
}
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(), "Clicked position: " + position, Toast.LENGTH_SHORT).show();
}
}
menu.xml v
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/background" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="10dp" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="54dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:src="@drawable/logo" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Menu"
android:textColor="#DC1700"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:textSize="25sp"
android:typeface="serif" />
</LinearLayout>
<View android:id="@+id/separator1"
android:background="#000000"
android:layout_width = "fill_parent"
android:layout_height="1dp"
android:layout_marginBottom="10dp" />
<ListView
android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#000000"
android:dividerHeight="1dp" >
<TextView android:id="@+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nothing" />
</ListView>
</LinearLayout>
*menu_row.xml v*
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="23dp"
android:shadowRadius="5"
android:shadowColor="#000000"
android:shadowDy="3"
android:shadowDx="3"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp" />
Yeah, this is the problem.
It should be
Fix it and check, report back if the problem persists.
Update: I can’t believe I didn’t see it earlier 😛
You have a
TextViewinside theListview. This is not possible. If you intend to use the “Empty View” Have them both inside a LinearLayout/FrameLayout. But the Listview cannot enclose the TextView, they should lie next to each other. Then, make sure you set the “Empty View” property in the “ListView” to the id of the Textview in this case@android:id/empty(please correct this as well)