我有问题。它给出错误“您必须为TextView提供资源ID”。问题出在哪里,谢谢。
SoundList类;
public class SoundList extends ListActivity {
int [] soundfile;
MediaPlayer mediaPlayer;
final Activity act = this;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.list_item);
soundfile= new int[] {R.raw.sound1,R.raw.sound2};
String[] newsounds = getResources().getStringArray(R.array.newsounds);
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, android.R.id.list, newsounds));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getApplicationContext(), SingleListItem.class);
intent.putExtra("position", position);
startActivity(intent);
}
});
LinearLayout layout = (LinearLayout)findViewById(R.id.reklam);
AdView adView = new AdView(this, AdSize.BANNER, "....");
layout.addView(adView);
AdRequest request = new AdRequest();
//request.setTesting(true);
adView.loadAd(request);
}
List_item.xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="18dip"
android:textStyle="bold" >
</ListView>
<LinearLayout
android:id="@+id/reklam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
错误日志;
11-17 01:43:34.110: E/ArrayAdapter(5118): You must supply a resource ID for a TextView
11-17 01:43:34.150: E/AndroidRuntime(5118): FATAL EXCEPTION: main
11-17 01:43:34.150: E/AndroidRuntime(5118): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.widget.AbsListView.obtainView(AbsListView.java:2040)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.widget.ListView.onMeasure(ListView.java:1155)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.view.View.measure(View.java:12881)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.widget.LinearLayout.measureVertical(LinearLayout.java:676)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.view.View.measure(View.java:12881)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.view.View.measure(View.java:12881)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.widget.LinearLayout.measureVertical(LinearLayout.java:676)
11-17 01:43:34.150: E/AndroidRuntime(5118): at a android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.view.View.measure(View.java:12881)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
11-17 01:43:34.150: E/AndroidRuntime(5118): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2111)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.view.View.measure(View.java:12881)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.os.Handler.dispatchMessage(Handler.java:99)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.os.Looper.loop(Looper.java:137)
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.app.ActivityThread.main(ActivityThread.java:4441)
11-17 01:43:34.150: E/AndroidRuntime(5118): at java.lang.reflect.Method.invokeNative(Native Method)
11-17 01:43:34.150: E/AndroidRuntime(5118): at java.lang.reflect.Method.invoke(Method.java:511)
11-17 01:43:34.150: E/AndroidRuntime(5118): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-17 01:43:34.150: E/AndroidRuntime(5118): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-17 01:43:34.150: E/AndroidRuntime(5118): at dalvik.system.NativeStart.main(Native Method)
11-17 01:43:34.150: E/AndroidRuntime(5118): Caused by: java.lang.ClassCastException: android.widget.ListView cannot be cast to android.widget.TextView
11-17 01:43:34.150: E/AndroidRuntime(5118): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:382)
11-17 01:43:34.150: E/AndroidRuntime(5118): ... 32 more
答案 0 :(得分:5)
应该是这样的::
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:textSize="20px" >
</TextView>
</LinearLayout>
在您的主要代码中: R.id.lable是您应该在list_item中拥有的textview的ID。
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list_item.xml, R.id.label, values);
setListAdapter(adapter);
你猥亵地做错了,我建议看一些教程:
制作名为
的新XML文件list_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="18dip"
android:textStyle="bold" >
</ListView>
<LinearLayout
android:id="@+id/reklam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
留下其他东西,它应该是:
setContentView(R.layout.list_layout);