【发布时间】:2016-10-19 13:24:45
【问题描述】:
我正在尝试为列表视图使用自定义布局,以及从 res/strings 中提取的字符串数组。起初我尝试使用
adapter = ArrayAdapter.createFromResource(this,R.array.android_versions,R.layout.list_view_custom)
但是收到一条错误消息,您必须为 textview 提供资源 ID,模拟器将在该资源 ID 上关闭。所以我尝试使用 newArrayAdapter,如下面的代码所示,使用第 4 行来获取我的字符串数组。由于在我尝试使用自定义布局之前这一切都有效,我认为问题出在第 4、第 9 和第 10 行。
public class ListViewActivity extends AppCompatActivity {
ListView listview;
ArrayAdapter<String> adapter;
List<String> android_versions = Arrays.asList(getResources().getStringArray(R.array.android_versions));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
listview = (ListView)findViewById(R.id.listview);
adapter = new ArrayAdapter<>(this,R.layout.list_view_custom,R.id.listview_custom,android_versions);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(),parent.getItemAtPosition(position) + " is selected",Toast.LENGTH_LONG).show();
}
});
}
}
这里是ListViewActivity的布局,content_list_view.,是通过activity_list_view获取的。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.andrewtakao.testapp.ListViewActivity"
tools:showIn="@layout/activity_list_view">
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</RelativeLayout>
这是我试图在 ListView 中实现的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/listview_custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:textAppearance="?android:textAppearanceLarge"
android:text="@string/apple"
android:gravity="center_horizontal"/>
</LinearLayout>
最后这是我最新的错误信息:
-18 18:51:17.682 12567-12567/com.example.andrewtakao.testapp I/Choreographer:跳过 112 帧!该应用程序可能也在做 在它的主线程上做了很多工作。 10-18 18:55:11.808 12567-12567/com.example.andrewtakao.testapp D/AndroidRuntime:关闭 关闭 VM 10-18 18:55:11.809 12567-12567/com.example.andrewtakao.testapp E/AndroidRuntime:致命异常:主要 进程:com.example.andrewtakao.testapp,PID:12567 java.lang.RuntimeException:无法实例化活动 组件信息{com.example.andrewtakao.testapp/com.example.andrewtakao.testapp.ListViewActivity}: java.lang.NullPointerException:尝试调用虚拟方法 'android.content.res.Resources android.content.Context.getResources()' 在空对象引用上 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在 android.app.ActivityThread.-wrap11(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:148) 在 android.app.ActivityThread.main(ActivityThread.java:5417) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 引起:java.lang.NullPointerException:尝试调用虚拟 方法'android.content.res.Resources android.content.Context.getResources()' 在空对象引用上 在 android.content.ContextWrapper.getResources(ContextWrapper.java:87) 在 android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81) 在 android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:551) 在 com.example.andrewtakao.testapp.ListViewActivity.(ListViewActivity.java:20) 在 java.lang.Class.newInstance(本机方法) 在 android.app.Instrumentation.newActivity(Instrumentation.java:1067) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在 android.app.ActivityThread.-wrap11(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:148) 在 android.app.ActivityThread.main(ActivityThread.java:5417) 在 java.lang.reflect.Method.invoke(Native Method)
谁能解释我做错了什么?非常感谢。
【问题讨论】:
标签: android listview android-arrayadapter