【问题标题】:"Null Object Reference" Error While Passing a Serializable Object To Fragment [duplicate]将可序列化对象传递给片段时出现“空对象引用”错误[重复]
【发布时间】:2017-04-03 01:27:22
【问题描述】:

我一直在研究这个并尝试了几个小时。在这篇文章之后,我举了一个如何将参数传递给片段的示例: 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?
  • 快速高效且易于实施
  • 试试 args.putSerializable("Player1", (Serializable) player1);并检查它是否有效

标签: java android android-fragments nullpointerexception


【解决方案1】:

试试这个

player1 = (Player)getActivity().getIntent().getSerializableExtra("Player1");

【讨论】:

    猜你喜欢
    • 2011-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 2016-07-12
    • 1970-01-01
    • 2013-11-06
    相关资源
    最近更新 更多