这是我的主要课程:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Context con;
protected void onClickCityBreak(View v) {
String [] arrayCityBreak = con.getResources().getStringArray(R.array.citybreak);
Intent myintent = new Intent(MainActivity.this, ActivityTo.class);
myintent.putExtra("PLACES",arrayCityBreak);
startActivity(myintent);
}
}
这是我想将myintent数组发送到的类:
public class ActivityTo extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String[] arrayCityBreak = getIntent().getStringArrayExtra("PLACES");
ArrayAdapter<String> adapterCityBreak = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayCityBreak);
ListView myview = getListView();
myview.setAdapter(adapterCityBreak);
}
}
我的AndroidManifest.xml文件:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityTo">
</activity>
我找不到这个问题。当我单击按钮以获得新意图时,我的应用程序关闭。这是因为我尝试发送String [],但我不明白为什么不起作用。
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ivarrreyna.assignment, PID: 2626
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at com.ivarrreyna.assignment.MainActivity.onClickCityBreak(MainActivity.java:24)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
申请已终止。
答案 0 :(得分:1)
没有必要传递数组,因为ActivityTo
也是一个活动所以也可以在这里获取资源数组
public class ActivityTo extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
String [] arrayCityBreak = getResources().getStringArray(R.array.citybreak);
ArrayAdapter<String> adapterCityBreak = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayCityBreak);
ListView myview = getListView();
myview.setAdapter(adapterCityBreak);
}
}
要删除当前错误,
要么这样做
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
con=this;
}
或
在MainActivity
中使用此功能,因为con
未初始化
String [] arrayCityBreak = getResources().getStringArray(R.array.citybreak);
而不是
String [] arrayCityBreak = con.getResources().getStringArray(R.array.citybreak);
答案 1 :(得分:0)
当我点击按钮以获得新意图时,我的应用程序关闭。这是因为String []我尝试发送,但我不明白为什么赢了。
由con
引起,因为上下文为空:
Context con;
使用它:
String [] arrayCityBreak = this.getResources().getStringArray(R.array.citybreak);
或
String [] arrayCityBreak = getResources().getStringArray(R.array.citybreak);