-1
我一直在研究這個問題並嘗試幾個小時。我已經在這篇文章後採取瞭如何傳遞參數到一個片段的例子: Send data from activity to fragment in android將可序列化對象傳遞給片段時發生「空對象引用」錯誤
我想從活動傳遞一個自定義對象,到一個片段。我選擇使用,以實現序列化和傳遞對象:
bundle.putSerializable("key", Serializable).
,我發現了錯誤
java.lang.NullPointerException:
嘗試在空對象引用調用虛方法java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)
當我試圖設置我的對象等於從活動傳入的參數。
這裏是我的對象
public class Player implements Serializable {
int attack;
private int tempAttack;
int defense;
private int tempDefense;
int pop;
int level;
String t1Evo;
String t2Evo;
String t3Evo;
public Player() {
attack = 0;
tempAttack = 0;
defense = 0;
tempDefense = 0;
pop = 20;
level = 1;
t1Evo = null;
t2Evo = null;
t3Evo = null;
}
}
那麼這裏就是我的活動從我傳遞,我最終想通過我的對象的所有四個,但是我現在我試圖得到至少一個順利通過。
public class GameScreen extends FragmentActivity {
// Anytime you need to grab data from the players,
// this screen will hold them.
private Player player1;
private Player player2;
private Player player3;
private Player player4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_game_screen);
// Instantiate the player objects
player1 = new Player();
player2 = new Player();
player3 = new Player();
player4 = new Player();
// put the objects to send to our fragment in a bundle
PlayerInformation newFragment = new PlayerInformation();
Bundle args = new Bundle();
args.putSerializable("Player1", player1);
newFragment.setArguments(args);
// start transaction
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Replace whatever is in the fragment_player_information view with this fragment,
transaction.add(R.id.playerInformation, newFragment);
transaction.commit();
}
}
然後其拋出空指針異常的片段,我曾表示與線「 - >」 包com.cs246.spencer.naturalselection;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.io.Serializable;
/**
* Fragment that displays the Player information on GameScreen
*/
public class PlayerInformation extends Fragment {
Context context;
Player player1;
Player player2;
Player player3;
Player player4;
public PlayerInformation(){
this.context=context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// grab the objects passed from GameScreen
Bundle args = getArguments();
--> player1 = (Player) args.getSerializable("Player1");
player1 = (Player) getArguments().getSerializable("Player1");
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_player_information, container, false);
}
public void Update() {
TextView player1Pop = (TextView) ((Activity)context).findViewById(R.id.player1Pop);
player1Pop.setText(player1.pop);
}
}
你爲什麼不使用parceable? –
其快速高效且易於實現 –
Try args.putSerializable(「Player1」,(Serializable)player1);並檢查它會工作 – Vickyexpert