gpt4 book ai didi

java - 切换到 Activity 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 22:45:14 26 4
gpt4 key购买 nike

我遇到一个问题:如果我尝试切换到具有以下代码的类,则会收到 NullPointerException:

aw1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {


if(Saver.getStage()==Saver.getGroup()/*&&Saver.getPoints()!=0*/)
{ Saver.setPoints(Saver.getPoints()+20);
Intent myIntent = new Intent(view.getContext(), Finish.class);
startActivityForResult(myIntent, 0);}
if(Stages.check(Saver.getStage())==1)
{Saver.setPoints(Saver.getPoints()+20);}

if(Saver.getStage()<8)
{Saver.setStage(Saver.getStage()+1);}
else
{Saver.setStage(0);}



Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();


}

});

我想切换到 Finish.class

Finish.java代码:

package com.example.romrallye;

import com.example.romrallye.MainActivity;
import com.example.romrallye.R;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;

public class Finish extends MainActivity {

private TextView punkte;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.finish_main);
punkte = (TextView) findViewById(R.id.txtpunkte);
punkte.setText(Saver.getPoints());



Button next = (Button) findViewById(R.id.bt_back4);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}

});}



@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

}

这是来自 LogCat 的错误:

enter image description here

最佳答案

如果值不是资源,则无法使用 setText(int)
使用

punkte.setText(String.valueOf(Saver.getPoints()));

而不是

punkte.setText(Saver.getPoints());

关于java - 切换到 Activity 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25471872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com