gpt4 book ai didi

android - ListView 中的 textview.setText() 导致强制关闭

转载 作者:行者123 更新时间:2023-11-29 00:40:16 25 4
gpt4 key购买 nike

这是我的代码,我知道某处有一个小错误,但作为 Android 的菜鸟,我无法弄清楚。我已完成搜索,但无济于事。

SimpleListActivity.java:

public class SimpleListActivity extends ListActivity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new myArrayAdapter(this, COUNTRIES));

ListView lv = getListView();
lv.setTextFilterEnabled(true);

lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}

static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
"Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
"Armenia", "Aruba", "Australia", "Austria"
};
}

主.xml:

<?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" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</CheckBox>

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView">
</TextView>

</LinearLayout>

myArrayAdapter.java:

public class myArrayAdapter extends ArrayAdapter<String> {

private final Context context;
private final String[] values;

public myArrayAdapter(Context context, String[] values) {
super(context, R.layout.list_item, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View rowView = inflater.inflate(R.layout.list_item, parent, false);

TextView textView = (TextView) rowView.findViewById(R.id.textView1);
CheckBox checkbox = (CheckBox) rowView.findViewById(R.id.checkBox1);
textView.setText(values[position]);
return rowView;
}

}

我不明白为什么我们要使用主布局作为 ContentView。我的意思是官方 ListView 教程不使用它,列表工作得很好。但是我在网上发现,当我使用findViewById时,我必须使用setContentView(R.layout.main);。我不明白如果id不在主视图中,那我为什么要使用它。

此外,此应用强制关闭。我已将问题缩小到 textView.setText(values[position]);。我哪里错了?

更新:我删除了 setContentView,因为它给出了另一个错误,解决方案发布在这里:ListView whose id attribute is 'android.R.id.list' Error when I have the ListView id set correctly

这是删除 setContentView 后的 logcat:

03-24 10:55:01.558: E/AndroidRuntime(7279): FATAL EXCEPTION: main
03-24 10:55:01.558: E/AndroidRuntime(7279): java.lang.NullPointerException
03-24 10:55:01.558: E/AndroidRuntime(7279): at com.deepakmittal.simplelist.myArrayAdapter.getView(myArrayAdapter.java:32)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.AbsListView.obtainView(AbsListView.java:1467)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.ListView.makeAndAddView(ListView.java:1745)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.ListView.fillDown(ListView.java:670)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.ListView.fillFromTop(ListView.java:727)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.ListView.layoutChildren(ListView.java:1598)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.AbsListView.onLayout(AbsListView.java:1273)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.View.layout(View.java:7192)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.ViewRoot.performTraversals(ViewRoot.java:1145)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.os.Looper.loop(Looper.java:130)
03-24 10:55:01.558: E/AndroidRuntime(7279): at android.app.ActivityThread.main(ActivityThread.java:3835)
03-24 10:55:01.558: E/AndroidRuntime(7279): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 10:55:01.558: E/AndroidRuntime(7279): at java.lang.reflect.Method.invoke(Method.java:507)
03-24 10:55:01.558: E/AndroidRuntime(7279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
03-24 10:55:01.558: E/AndroidRuntime(7279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
03-24 10:55:01.558: E/AndroidRuntime(7279): at dalvik.system.NativeStart.main(Native Method)

第32行对应textView.setText(values[position]);

最佳答案

ListActivity 有一个默认布局,由位于屏幕中央的单个全屏列表组成。但是,如果您愿意,您可以通过在 onCreate() 中使用 setContentView() 设置您自己的 View 布局来自定义屏幕布局。要做到这一点,您自己的 View 必须包含一个 ListView 对象,其 ID 为“@+id/list”……所以只有当您需要自定义布局时,您才应该使用 setContentView() 否则……不需要使用 R。 layout.main ..现在强制关闭你的应用程序..你应该发布logcat ......这样就很容易找出错误所在..并且我认为没有任何错误...除了更改

  android:id="@+id/listView1" in main.xml

to

android:id="@+id/list"

因为我复制了代码并尝试了..它对我来说很好用..

关于android - ListView 中的 textview.setText() 导致强制关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9849060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com