1

主なアクティビティでは、次のように String[] 名を送信します。

private void sendNames() {

    Bundle b=new Bundle();
    b.putStringArray("key", names);
    Intent i=new Intent(this, ListFriendsFragment.class);
    i.putExtras(b);

}

名前を送信すると、100%空ではなく、このコードをメソッドに入れ、名前を取得した後に呼び出しました。

アクティビティでは、string[] を受け取りたいです。次のように取得します。

 names = this.getIntent().getExtras().getStringArray("key");

メインアクティビティと文字列を受け取りたいアクティビティの両方で、names次のように宣言されています。

private String[] names;

namesアプリケーションがクラッシュするはずのアクティビティを開始すると、次のようになります。

Caused by: java.lang.NullPointerException
at com.utm.course.friendslist.ListFriendsFragment.PrintNames(ListFriendsFragment.java:26)
at com.utm.course.friendslist.ListFriendsFragment.onCreate(ListFriendsFragment.java:20)

私は何を間違っていますか?


アップデート

これらは私が使用する部品ですIntent

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (currentSession != null) {
        currentSession.onActivityResult(this, requestCode, resultCode, data);
    }
}
...
private void sendNames() {
    Log.d("sendNames", "started");
    Bundle b=new Bundle();
    b.putStringArray(key, names);
    Intent i=new Intent(this, ListFriendsFragment.class);
    i.putExtras(b);
}
...
 private void listFriends() {
    Log.d("Activity", "List Friends Activity Starting");
    Intent i=new Intent(MainActivity.this,ListFriendsFragment.class);
    startActivity(i);
    finish();
}
4

3 に答える 3

0

送信

Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);

受信

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
于 2013-10-12T19:12:42.237 に答える
0

このようにしてください:

仮定、文字列[]名;

Intent intent = new Intent(this, ListFriendsFragment.class);
intent.putExtra("key", names);
startActivity(intent);

次回の活動では、

Intent intent = getIntent();
String[] names = intent.getStringArrayExtra("key");
于 2013-10-12T17:18:51.397 に答える