我制作了一个简单的点击游戏应用。我知道它糟糕的编程,但我真的不明白,为什么我的应用程序在尝试打开其他布局后不断崩溃..我刚刚开始使用Android Studio,所以期待我不会看到问题,尽管它可能很明显。< / p>
MainActivity
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.content.Intent;
public class MainActivity extends AppCompatActivity {
public static final int REQUEST_CODE_CURRENTCLICKS = 10;
public int amountClicks = 0;
public int incrementAmount = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnClick = (Button) findViewById(R.id.btnClick);
final TextView tvAmountClicks = (TextView) findViewById(R.id.tvAmoutClicks);
Button btnShop = (Button) findViewById(R.id.btnShop);
amountClicks = getIntent().getIntExtra("currentClicks", 0);
incrementAmount = getIntent().getIntExtra("upgradedClicks", 1);
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
amountClicks +=incrementAmount;
tvAmountClicks.setText(""+amountClicks);
}
});
btnShop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),ShopActivity.class);
intent.putExtra("currentClicks",amountClicks);
startActivityForResult(intent, REQUEST_CODE_CURRENTCLICKS);
}
});
}
}
&#13;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class ShopActivity extends AppCompatActivity {
public int currentClicks;
public int incrementAmount = 1;
public int upgradedClicks = 1;
public int upgradeCost = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop);
currentClicks = getIntent().getIntExtra("currentClicks",0);
TextView tvCurrentClicks = (TextView) findViewById(R.id.tvAmoutClicks);
tvCurrentClicks.setText(""+currentClicks);
Button btnUpgrade = (Button) findViewById(R.id.btnUpgrade);
btnUpgrade.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(upgradeCost > currentClicks)
{
Context context = getApplicationContext();
CharSequence text = "You don't have enough Clicks";
int duration = Toast.LENGTH_SHORT;
Toast cantUpgrade = Toast.makeText(context, text,duration);
cantUpgrade.show();
}
else
{
currentClicks =- upgradeCost;
upgradedClicks =+ incrementAmount;
}
Intent intent = new Intent();
intent.putExtra("currentClicks",currentClicks);
intent.putExtra("upgradedClicks",upgradedClicks);
setResult(RESULT_OK, intent);
}
});
}
}
&#13;
感谢您的帮助!
编辑: 登录
03-08 21:24:08.263 14893-14893/de.iwanow.zilly.zillysclicker E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.iwanow.zilly.zillysclicker, PID: 14893
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.iwanow.zilly.zillysclicker/de.iwanow.zilly.zillysclicker.ShopActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at de.iwanow.zilly.zillysclicker.ShopActivity.onCreate(ShopActivity.java:26)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
答案 0 :(得分:0)
据我从日志中了解,问题出在以下几行:
TextView tvCurrentClicks = (TextView) findViewById(R.id.tvAmoutClicks);
tvCurrentClicks.setText(""+currentClicks);
似乎tvCurrentClicks为null。
您确定ShopActivity的布局中是否存在tvAmoutClicks
ID?