我想從一個活動傳遞一個字符串數組到另一個,這是一個ListActivity。我需要這個數組來填充ListActivity。 這是代碼,但它不工作,它崩潰: 頭等艙,從那裏我想送:錯誤,當我嘗試使用putExtra傳遞一個數組,並在另一個活動中獲取它
package com.example.browser3;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Settings2 extends Activity{
EditText etAdress;
Button bAdd;
Button bRemove;
Button bMenu;
String[] array={};
int counter=0;
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.settings2);
super.onCreate(savedInstanceState);
bMenu=(Button)findViewById(R.id.bMenu);
bMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
clickHome();
}
});
etAdress=(EditText) findViewById(R.id.etAdress);
bAdd=(Button) findViewById(R.id.bAdd);
bAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
array[counter]=etAdress.getText().toString();
counter++;
Bundle b=new Bundle();
b.putStringArray("key", array);
Intent intent = new Intent(Settings2.this, Favorite.class);
intent.putExtras(b);
startActivity(intent);
}
});
private void clickHome(){
startActivity(new Intent("com.example.browser3.MENU"));
}
}
而且這是代碼爲第二類,它應該得到:
package com.example.browser3;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStreamWriter;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Favorite extends ListActivity {
String[] elements={};
ArrayAdapter<String> adapter;
HomePage object=new HomePage();
public void setElements(int position,String element) {
elements[position]=element;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// elements = getIntent().getStringArrayExtra("name");
setListAdapter(new ArrayAdapter<String>(Favorite.this , android.R.layout.simple_list_item_1 , elements));
Intent intent = getIntent();
elements=intent.getExtras().getStringArray("key");
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
try{
Class ourClass = Class.forName("com.example.browser3.HomePage");// ce e aici aia porneste la click
Intent ourIntent = new Intent(Favorite.this , ourClass);
String s=elements[position];
writeFile("history.txt",s);
ourIntent.putExtra("name", s);
startActivity(ourIntent);
}
catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public void writeFile(String fileName, String x) {
try {
OutputStreamWriter out = new OutputStreamWriter(openFileOutput(
fileName, MODE_APPEND));
out.write(x + "\n");
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
到目前爲止,我看過的所有問題都無法解決我的問題。
當我開始收藏活動時,它崩潰。這是logcat的:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.browser3/com.example.browser3.Favorite}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.browser3.Favorite.onCreate(Favorite.java:31)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more
您可以發佈它與崩潰例外。它是一個空指針或其他東西? – startoftext 2014-09-05 14:52:06
我不知道如何得到你的答案... – 2014-09-05 14:54:21
如果你很善良,我可以給你這個項目,我絕望 – 2014-09-05 14:54:43