私はこれを研究し、何時間も試みてきました。私はこの記事の後半で、フラグメントに引数を渡す方法の例を取ってきました。 Send data from activity to fragment in androidフラグメント化するシリアライズ可能オブジェクトを渡す際の "NULLオブジェクト参照"エラー
アクティビティからカスタムオブジェクトをフラグメントに渡そうとしています。私はSerializableを実装して使用してオブジェクトを渡すことにしました:
bundle.putSerializable("key", Serializable).
私はエラーにI
nullオブジェクト参照上の仮想メソッドjava.io.Serializable android.os.Bundle.getSerializable(java.lang.String)
を起動する
は、ここではその後、私のオブジェクト
java.lang.NullPointerException:
試みを取得しています私のオブジェクトをアクティビティから引き渡された引数と同じに設定しようとしています。ここ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; } }
で、私は最終的に私のオブジェクトのすべての4つを通過したい私が渡していたから、私の活動ですが、私は今の私は、少なくとも一つを取得しようとしています首尾よく通過する。
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(); } }
そしてNULLポインタ例外をスローフラグメントが、私はとのラインを示してきました「 - >」 パッケージ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); } }
を試してみてください? –
その高速かつ効率的で実装が容易 –
args.putSerializable( "Player1"、(Serializable)player1);を試してください。それが動作することを確認してください – Vickyexpert