2014-09-05 11 views
-1

我想從一個活動傳遞一個字符串數組到另一個,這是一個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 
+0

您可以發佈它與崩潰例外。它是一個空指針或其他東西? – startoftext 2014-09-05 14:52:06

+0

我不知道如何得到你的答案... – 2014-09-05 14:54:21

+0

如果你很善良,我可以給你這個項目,我絕望 – 2014-09-05 14:54:43

回答

0

看到你的代碼後,我得到了空指針的原因

您正在試圖讓喜愛的負載捆價值,因爲你分配束值是錯誤的只有在設置2頁面,如果用戶試圖從主頁加載它正在尋找捆綁值,因爲你沒有分配任何東西在那裏它與空指針崩潰。因此,嘗試再傳一個字符串與價值活動,以便您可以檢查收藏夾頁面,其中來電,如果它來自主頁,則不做任何操作,如果它來自Settings2按照捆綁方式執行操作

0

也許吧會因爲您在安裝var元素之前使用元素而崩潰。

setListAdapter(new ArrayAdapter<String>(Favorite.this , android.R.layout.simple_list_item_1 , elements)); 
Intent intent = getIntent(); 
elements=intent.getExtras().getStringArray("key"); 

如果你刪除//因爲「名」是你StringArrayExtras

// elements = getIntent().getStringArrayExtra("name"); 
0

第31行最喜歡的活動是拋出空指針異常沒有按鍵,這將不起作用。 您不必使用包來傳遞價值。你可以簡單的使用意圖,設定2的活動是這樣的:

intent.putExtra("key", array); 
startActivity(intent); 

從你最喜歡的活動,你可以通過下面的代碼得到數組:

elements = new String[]; 
elements= getIntent().getStringArrayExtra("key"); 
相關問題