我目前正在尝试使用android studio中的列表视图创建主 - 详细信息页面。
我创建了列表视图,创建了一个要放入其中的字符串数组,称为适配器,所有代码看起来都很好。
但是,当我在包含列表视图的应用程序中打开活动时,应用程序崩溃并说“不幸的是应用程序已停止工作”。
我会在下面附上错误代码和我的实际代码。任何帮助将不胜感激。
我的代码
public class PlaceList extends AppCompatActivity {
ListView listView;
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(PlaceList.this,
android.R.layout.simple_list_item_1,
getResources().getStringArray(R.array.countries));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_list);
listView =(ListView)findViewById(R.id.Place_List);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView,View view,int i,long l){
Intent intent = new Intent(PlaceList.this,DetailsActivity.class);
intent.putExtra("CountryName",listView.getItemAtPosition(i).toString());
startActivity(intent);
}
});
listView.setAdapter(mAdapter);
}
**关于崩溃的错误报告**
03-01 15:21:14.443 13699-13699/com.example.bradwaterhouse.discovernottingham
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bradwaterhouse.discovernottingham, PID: 13699
java.lang.RuntimeException: Unable to instantiate activity
java.lang.NullPointerException:
Attempt to invoke virtual method 'android.content.res.Resources
android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3132)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:92)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
at
android.support.v7.app.AppCompatActivity.getResources
(AppCompatActivity.java:542)
at com.example.bradwaterhouse.discovernottingham.PlaceList.<init>
(PlaceList.java:17)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1096)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3122)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
at android.app.ActivityThread.access$1100(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7331)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:0)
您需要onCreate()
方法中的初始适配器。
答案 1 :(得分:0)
如上文评论所述
mAdapter = new ArrayAdapter<String>(PlaceList.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.countries));
只需在oncreate中写下这一行
原因
您只能在活动生命周期的onCreate()
或更高版本中将您的活动用作资源上下文
信用laalto