我正在尝试从ShoutActivity做一个意图,将活动扩展到MyListActivity,在Click上扩展ListActivity
应用程序在达到意图时到达
代码:
第一项活动:
public class ShoutActivity extends Activity implements View.OnClickListener{
/** Called when the activity is first created. */
private Button b1;
private Button b2;
private EditText text_box;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button) this.findViewById(R.id.button1);
b1.setOnClickListener(this);
b2 = (Button) this.findViewById(R.id.button04);
b2.setOnClickListener(this);
//.....other stuff
}
public void onClick(View v) {
if (v==b1)
{
//not important
}
if(v==b2){
Intent intent = new Intent();
intent.setClass(ShoutActivity.this,MyListActivity.class);
);
Bundle myBundle = new Bundle();
myBundle.putStringArray("List", s1);
intent.putExtras(myBundle);
startActivity(intent);
}
}
main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<EditText android:layout_width="match_parent" android:id="@+id/editText1" android:layout_height="wrap_content">
<requestFocus></requestFocus>
</EditText>
<Button android:text="Shout !" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="View Shouts" android:id="@+id/button04" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="karim.mobi"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ShoutActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MyListActivity" android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar"/>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
</manifest>
第二项活动
package karim.mobi;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;
import android.widget.*;
import android.app.*;
public class MyListActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String s[];
s= savedInstanceState.getStringArray("List");
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s));
getListView().setTextFilterEnabled(true);
}
}
错误logcat:
04-21 21:29:31.177: ERROR/AndroidRuntime(789): FATAL EXCEPTION: main
04-21 21:29:31.177: ERROR/AndroidRuntime(789): java.lang.RuntimeException: Unable to start activity ComponentInfo{karim.mobi/karim.mobi.MyListActivity}: java.lang.NullPointerException
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.os.Looper.loop(Looper.java:123)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at java.lang.reflect.Method.invoke(Method.java:521)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at dalvik.system.NativeStart.main(Native Method)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): Caused by: java.lang.NullPointerException
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at karim.mobi.MyListActivity.onCreate(MyListActivity.java:21)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-21 21:29:31.177: ERROR/AndroidRuntime(789): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
你能告诉我这是什么问题吗?
请具体
答案 0 :(得分:1)
首先,您必须为MyListActivity设置内容视图。因此,您需要在 super.onCreate :
之后立即调用以下内容setContentView(R.layout.your_layout_name);
其次,你在同一个活动中读错了数组。
改变这个:
s= savedInstanceState.getStringArray("List");
对此:
s= getIntent().getExtras().getStringArray("List");
答案 1 :(得分:1)
使用它:
Resources res = getResources () ;
String [] s1 = res . getStringArray ( R . array . strs_array );
Intent intent = new Intent();
intent.setClass(ShoutActivity.this,MyListActivity.class);
);
Bundle myBundle = new Bundle();
myBundle.putStringArray("List", s1);
intent.putExtras(myBundle);
ShoutActivity.this.startActivity(intent);
和活动MyListActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle callerBundle = getIntent().getExtras();
String s[];
if(callerBundle !=null)
{
s=callerBundle.getStringArray("List");
setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s));
getListView().setTextFilterEnabled(true);
}