这是我的主要课程:
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)申请终止。
This is my main class:
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); } }And this is the class i want to send myintent array to:
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); } }My AndroidManifest.xml file:
<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>I can't find the problem in this. My application shuts down when i click the button to get to the new intent. It is because of the String[] i try to send, but I don't see why is won't work.
--------- 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)Application terminated.
最满意答案
没有必要传递数组,因为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);There is no need to pass array to , since ActivityTo is also an activity so can fetch the resources array here too
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); } }To remove current error,
either do this
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); con=this; }or
use this in MainActivity because con is not initialized
String [] arrayCityBreak = getResources().getStringArray(R.array.citybreak);instead of
String [] arrayCityBreak = con.getResources().getStringArray(R.array.citybreak);更多推荐
发布评论