具有意图、可解析和获取数据sqlite的空对象引用。 Everythink 是在不同布局的 onCreate 中编码的[重复]

问题描述 投票:0回答:1

我有一个空对象引用,但对象存在。

我的模型

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPlayerName() {
        return playerName;
    }

    public void setPlayerName(String playerName) {
        this.playerName = playerName;
    }

    public String getPlayerBirthday() {
        return playerBirthday;
    }

    public void setPlayerBirthday(String playerBirthday) {
        this.playerBirthday = playerBirthday;
    }

    public String getPlayerClub() {
        return playerClub;
    }

    public void setPlayerClub(String playerClub) {
        this.playerClub = playerClub;
    }
    public PlayerModel(int id, String playerName, String playerBirthday, String playerClub){
        this.id = id;
        this.playerName = playerName;
        this.playerBirthday = playerBirthday;
        this.playerClub = playerClub;
    }

    public PlayerModel(String playerName, String playerBirthday, String playerClub) {
        this.playerName = playerName;
        this.playerBirthday = playerBirthday;
        this.playerClub = playerClub;
    }

在我的玩家列表活动中

       public class PlayerList extends AppCompatActivity {
    ListView lvPlayerList;
    ArrayList<PlayerModel> arrayList;
    PlayerListAdapter adapter;
    DatabaseManager playerDb;
    ImageButton btnPLToHome,btnToAP;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player_list);

        playerDb = new DatabaseManager(this);
        arrayList=playerDb.getPlayerData();
        lvPlayerList = findViewById(R.id.lvPlayerList);
        btnPLToHome=findViewById(R.id.btnPLToHome);
        btnToAP=findViewById(R.id.btnToAP);

lvPlayerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PlayerModel playerModel=arrayList.get(position);
                Intent intent = new Intent(PlayerList.this, MainActivity.class);
                intent.putExtra("PLAYER",playerModel);
                startActivity(intent);
            }
        });

在我的主要活动中

     public class MainActivity extends AppCompatActivity {
    Button btnToMatch,btnToDataListActivity,btnToAddPlayer,btnToAddClub;
    int year,month,day;
    TextView tvMainPN,tvMainPB,tvMainPC;
    EditText etOpponentClub,etCategory,etDate;
    String playerName,opponent,date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnToMatch = findViewById(R.id.btnToMatch);
        btnToDataListActivity = findViewById(R.id.btnToDataListActivity);
        btnToAddPlayer=findViewById(R.id.btnToAddPlayer);
        btnToAddClub=findViewById(R.id.btnToAddClub);
        tvMainPN = findViewById(R.id.tvMainPN);
        tvMainPB = findViewById(R.id.tvMainPB);
        tvMainPC = findViewById(R.id.tvMainPC);
        etOpponentClub = findViewById(R.id.etOpponentClub);
        etCategory = findViewById(R.id.etCategory);
        etDate = findViewById(R.id.etDate);

        PlayerModel playerModel=(PlayerModel) getIntent().getExtras().getSerializable("PLAYER");
        tvMainPN.setText(playerModel.getPlayerName());
        tvMainPB.setText(playerModel.getPlayerBirthday());
        tvMainPC.setText(playerModel.getPlayerClub());

Logcat错误

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)' on a null object reference

我尝试了不同的解决方案来显示来自 sqlite 的数据,但没有任何效果。直接从数据库、意图、可分割的数据获取数据总是出现相同的错误。 我忘记了什么? 我想我必须初始化一些东西,但是什么以及在哪里?

有人有主意吗? 谢啦

java android android-sqlite
1个回答
0
投票

如果您从 PlayerList 活动中

start
MainActivity(该活动是从 MainActivity 启动的),则 MainActivity 的原始实例将被销毁,以允许启动 MainActivity 的实例,然后将启动 MainActivity 的新实例来代替原始实例(因此出现问题)。

您需要

finish
PlayerList 活动(假设它是从 MainActivity 启动的),以允许将控制权交还给调用/原始 MainActivity。

如果您需要将结果返回到 MainActivity 那么您应该编码并使用

startActivityForResult
请参阅 https://developer.android.com/training/basics/intents/result

© www.soinside.com 2019 - 2024. All rights reserved.