这是我第一次使用ListView,并尝试了很多方法使其可行。首先,我在xml文件下使用了一个字符串数组来指定“android:entries”,但是我无法激活onClickListener。另一种方法与我所用的方法相似,其中一些方法是我使用“扩展Acitvity”的方式。预先感谢帮助我。ListView无法显示
基本上,我想为列表上的每个项目使用自定义的textView行,并为每个可点击的行显示一个敬酒。我的问题是,当我尝试显示此活动时,应用程序会崩溃。我不确定问题出在哪里,因为没有任何语法错误。该错误信息是(对不起,我会放上一张图片,但我不能 - 新用户):
- 致命异常:主要
- 了java.lang.RuntimeException:无法启动活动ComponentInfo(EDU .. ...)
- 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
- 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2237)
- 在android.app.ActivityThread。访问600美元(ActivityTHread.java:139)
- 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1262)
- 在android.os.Handler.dispatchMessage(Handler.java:99)
- 在android.os.Looper.loop(Loooper。的java:154)
- 在android.app.ActivityThread.main(ActivityThread.java:4974)
- 在java.lang.reflect.Method.invokeNative(本机方法)
- 在java.lang.reflect.Method中.invoke(Method java:511)
- at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
- 在com.android.internal.os.ZygoteInit.main(ZygotaInit.java:551)
- 在dalvik.system.NativeStart.main(本机方法)
- 造成的:java.lang.UnsupportedOperationException:addView(查看,的LayoutParams)...
- 在android.widget.AdapterView.addView(AdapterView.java:471)
- 在android.view.LayoutInflater.rInflate(LayoutInflater.java:743)
- 在android.view。 LayoutInflater.rInflate(LayoutInflater.java:742)
- at android.view.LayoutInflater.rInflate(LayoutInflater.java:489)
- 在android.view.LayoutInflater.rInflate(LayoutInflater.java:396)
- 在android.view.LayoutInflater.rInflate(LayoutInflater.java:352)
- 在com.android.internal.policy.impl.PhoneWindows .setContentView(PhoneWindow.java ...)
- 在android.app.Activity.setContentView(Activity.java:1892)
- 在edu.nyp.wonderful.Menu.onCreate(Menu.java:14)
- at android.app.Activity.performCreate(Activity.java:4538)
- at android.app.Instrumentation.callActivityOnCreate(Instr umentation。Java的:1071)
- 在android.app.ActivityThread.performLaunchActivity(ActivityTHread.java:2158)
- ... 11个多行(我不能表现出更多的输入输出)
菜单。 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" />
请添加完整的logcat错误。 –
增加了更多的错误行谢谢 – user1661881
@ user1661881你是否从其他活动调用Menu(ListActivity)..就像使用按钮单击或某件事情? –