gpt4 book ai didi

Android 应用程序重新打开后崩溃

转载 作者:行者123 更新时间:2023-12-02 09:30:07 25 4
gpt4 key购买 nike

我不仅是 Android Studio 的新手,而且对于一般编程也是新手(作为一种爱好,我在过去一年左右的时间里一直在慢慢自学)。

我的 Android 应用程序遇到了一个问题,我无法找到答案 - 尽管我知道很多人都问过类似的问题,但我似乎无法充分理解该解决方案以使其适应我的需求特别的问题。编译后,我的应用程序将运行良好,并“正确”执行我希望它执行的所有操作(除了工具栏的一个小问题,但我稍后会讲到,哈哈)。

它没有做的一件事是在我关闭它后正确打开它。我可以转到主屏幕并返回应用程序,但如果我完全关闭应用程序并尝试重新打开,则会显示它“不断停止”。

现在,我认为向您提供我的代码(此应用程序中只有两个 Activity )和我的“logcat”会很有用。不过,我目前正在尝试阅读并了解如何首先检索我的“logcat”哈哈,所以希望我很快就能在这里找到它。

根据我的阅读,我的理解是,我的问题要么是我声明的全局变量,要么是我动态制作按钮时按钮 ID 的问题。我遇到有人对他们的“主题”有疑问,但我无法将其与我的特定立场联系起来。然而,如果其中任何一个是问题,我很难理解问题到底是什么。

这是我的主要 Activity ...

package com.example.getcoins;

import android.content.Intent;
import android.icu.text.DecimalFormat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.PointsGraphSeries;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

int intialCoins = 21;
int coins;
int buttonColor = android.graphics.Color.rgb(0, 150, 150);
int textColor = android.graphics.Color.rgb(255, 255, 255);
ArrayList<Integer> coinsOverTime = new ArrayList<Integer>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);

Intent intent = getIntent();
coins = intent.getIntExtra("COINS", 1);

if (intent.getExtras() != null)
{
Boolean winner = intent.getBooleanExtra("WINNER", true);
int gambleamount = intent.getIntExtra("GAMBLE", 1);
Bundle bundle = intent.getExtras();
coinsOverTime = bundle.getIntegerArrayList("COIN_HISTORY");

if (winner == true)
{
coins = coins + gambleamount;
coinsOverTime.add(coins);
}

if (winner == false && gambleamount == 1)
{
coins = coins;
coinsOverTime.add(coins);
}

else if(winner == false)
{
coins = coins - gambleamount;
coinsOverTime.add(coins);
}
}

else
{
coins = intialCoins;
coinsOverTime.add(coins);
}

String number = Integer.toString(coins);
double amount = Double.parseDouble(number);
DecimalFormat formatter = new DecimalFormat("#,###");
String formatted = formatter.format(amount);

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(formatted);

GraphView graph = (GraphView) findViewById(R.id.graph);
PointsGraphSeries<DataPoint> series = new PointsGraphSeries<>(generateData());
graph.addSeries(series);
series.setShape(PointsGraphSeries.Shape.POINT);
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setMinX(0);
graph.getViewport().setMaxX(coinsOverTime.size());

if (coins >= 0 && coins < 101)
{
Button getCoinButton = addButton("Get 1 coin");
getCoinButton.setId(R.id.one);
getCoinButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getOneCoin(v);
}
});
}

if (coins >= 11 && coins < 501)
{
Button getCoinButton = addButton("Get 10 coins");
getCoinButton.setId(R.id.ten);
getCoinButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getTenCoins(v);
}
});
}

if (coins >= 21 && coins < 2001)
{
Button getCoinButton = addButton("Get 20 coins");
getCoinButton.setId(R.id.twenty);
getCoinButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getTwentyCoins(v);
}
});
}

if (coins >= 101 && coins < 5001)
{
Button getCoinButton = addButton("Get 100 coins");
getCoinButton.setId(R.id.one_hundred);
getCoinButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getOneHundredCoins(v);
}
});
}

if (coins >= 501 && coins < 10001)
{
Button getCoinButton = addButton("Get 500 coins");
getCoinButton.setId(R.id.five_hundred);
getCoinButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getFiveHundredCoins(v);
}
});
}

if (coins >= 2001)
{
Button getCoinButton = addButton("Get 2,000 coins");
getCoinButton.setId(R.id.two_thousand);
getCoinButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getTwoThousandCoins(v);
}
});
}

if (coins >= 5001)
{
Button getCoinButton = addButton("Get 5,000 coins");
getCoinButton.setId(R.id.five_thousand);
getCoinButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getFiveThousandCoins(v);
}
});
}

if (coins >= 10001)
{
Button getCoinButton = addButton("Get 10,000 coins");
getCoinButton.setId(R.id.ten_thousand);
getCoinButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getTenThousandCoins(v);
}
});
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_items, menu);
return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.rules:
// User chose the "Settings" item, show the app settings UI...
return true;

case R.id.action_settings:
// User chose the "Favorite" action, mark the current item
// as a favorite...
return true;

case android.R.id.home:
return true;

default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}




public void getOneCoin(View view){
Intent intent = new Intent(this, gamble.class);
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("COIN_HISTORY", coinsOverTime);
intent.putExtra("GAMBLE", 1);
intent.putExtra("COINS", coins);
intent.putExtras(bundle);
startActivity(intent);
}

public void getTenCoins(View view){
Intent intent = new Intent(this, gamble.class);
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("COIN_HISTORY", coinsOverTime);
intent.putExtra("GAMBLE", 10);
intent.putExtra("COINS", coins);
intent.putExtras(bundle);
startActivity(intent);
}

public void getTwentyCoins(View view){
Intent intent = new Intent(this, gamble.class);
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("COIN_HISTORY", coinsOverTime);
intent.putExtra("GAMBLE", 20);
intent.putExtra("COINS", coins);
intent.putExtras(bundle);
startActivity(intent);
}

public void getOneHundredCoins(View view){
Intent intent = new Intent(this, gamble.class);
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("COIN_HISTORY", coinsOverTime);
intent.putExtra("GAMBLE", 100);
intent.putExtra("COINS", coins);
intent.putExtras(bundle);
startActivity(intent);
}

public void getFiveHundredCoins(View view){
Intent intent = new Intent(this, gamble.class);
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("COIN_HISTORY", coinsOverTime);
intent.putExtra("GAMBLE", 500);
intent.putExtra("COINS", coins);
intent.putExtras(bundle);
startActivity(intent);
}

public void getTwoThousandCoins(View view){
Intent intent = new Intent(this, gamble.class);
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("COIN_HISTORY", coinsOverTime);
intent.putExtra("GAMBLE", 2000);
intent.putExtra("COINS", coins);
intent.putExtras(bundle);
startActivity(intent);
}

public void getFiveThousandCoins(View view){
Intent intent = new Intent(this, gamble.class);
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("COIN_HISTORY", coinsOverTime);
intent.putExtra("GAMBLE", 5000);
intent.putExtra("COINS", coins);
intent.putExtras(bundle);
startActivity(intent);
}

public void getTenThousandCoins(View view){
Intent intent = new Intent(this, gamble.class);
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("COIN_HISTORY", coinsOverTime);
intent.putExtra("GAMBLE", 10000);
intent.putExtra("COINS", coins);
intent.putExtras(bundle);
startActivity(intent);
}

public Button addButton(String name)
{
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
Button newCoinButton = new Button(this);
newCoinButton.setText(name);
newCoinButton.setTextSize(30);
//newCoinButton.setBackgroundColor(buttonColor);
newCoinButton.setTextColor(textColor);
//newCoinButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(newCoinButton);

return newCoinButton;
}

public DataPoint[] generateData()
{
int size = coinsOverTime.size();
DataPoint[] values = new DataPoint[size];
for (int i=0; i<size; i++)
{
int y1 = coinsOverTime.get(i);
DataPoint v = new DataPoint(i, y1);
values[i] = v;
}

return values;
}
}

这是它所涉及的 Gamble Activity ...

package com.example.getcoins;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;

public class gamble extends AppCompatActivity {

ArrayList<Integer> buttonValues = getButtonValues();
//ArrayList<Integer> coinHistory = getCoinHistory();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gamble);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
ActionBar ab = getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(false);

TextView textViewLeft = (TextView) findViewById(R.id.textView2);
TextView textViewRight = (TextView) findViewById(R.id.textView3);
TextView textViewCoins = (TextView) findViewById(R.id.textView4);

textViewCoins.setText("Good Luck!");


if (buttonValues.get(0) == 0)
{
textViewLeft.setText("Winner");
}

else
{
textViewLeft.setText("Loser");
}

if (buttonValues.get(1) == 0)
{
textViewRight.setText("Winner");
}

else
{
textViewRight.setText("Loser");
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_items, menu);
return true;
}

public ArrayList<Integer> getButtonValues()
{
ArrayList<Integer> buttonvalues = new ArrayList<Integer>();
buttonvalues.add(0);
buttonvalues.add(1);
Collections.shuffle(buttonvalues);
return buttonvalues;
}

public void leftButtonClick(View view)
{
if (buttonValues.get(0) == 0)
{
winner();
}

else
{
loser();
}
}

public void rightButtonClick(View view)
{
if (buttonValues.get(1) == 0)
{
winner();
}

else
{
loser();
}
}

public void winner()
{
Intent oldintent = getIntent();
Bundle oldbundle = oldintent.getExtras();
int gambleamount = oldintent.getIntExtra("GAMBLE", 1);
int coins = oldintent.getIntExtra("COINS", 1);
ArrayList<Integer> coinHistory = oldbundle.getIntegerArrayList("COIN_HISTORY");

Intent intent = new Intent(this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("COIN_HISTORY", coinHistory);
intent.putExtra("WINNER", true);
intent.putExtra("GAMBLE", gambleamount);
intent.putExtra("COINS", coins);
intent.putExtras(bundle);
startActivity(intent);
}

public void loser()
{
Intent oldintent = getIntent();
Bundle oldbundle = oldintent.getExtras();
int gambleamount = oldintent.getIntExtra("GAMBLE", 1);
int coins = oldintent.getIntExtra("COINS", 1);
ArrayList<Integer> coinHistory = oldbundle.getIntegerArrayList("COIN_HISTORY");

Intent intent = new Intent(this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("COIN_HISTORY", coinHistory);
intent.putExtra("WINNER", false);
intent.putExtra("GAMBLE", gambleamount);
intent.putExtra("COINS", coins);
intent.putExtras(bundle);
startActivity(intent);
}
}

我感谢任何人能够提供的任何帮助。

卢克

更新:这是我的 logcat...或者至少我认为是相关部分。崩溃发生后,我捕获了它给我的文字。

04-06 16:10:39.091 3897-3897/? I/art: Late-enabling -Xcheck:jni
04-06 16:10:39.322 3897-3897/com.example.getcoins W/System: ClassLoader referenced unknown path: /data/app/com.example.getcoins-1/lib/arm64
04-06 16:10:39.363 3897-3897/com.example.getcoins I/InstantRun: Starting Instant Run Server for com.example.getcoins
04-06 16:10:42.158 3897-3897/com.example.getcoins W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-06 16:10:42.643 3897-3897/com.example.getcoins D/AndroidRuntime: Shutting down VM
04-06 16:10:42.645 3897-3897/com.example.getcoins E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.getcoins, PID: 3897
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.getcoins/com.example.getcoins.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2671)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2736)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6154)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at com.example.getcoins.MainActivity.onCreate(MainActivity.java:50)
at android.app.Activity.performCreate(Activity.java:6683)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1140)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2624)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2736) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6154) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757) 

更新:

首先感谢您的回复!这对我来说非常有教育意义!我想按照我目前的逻辑来判断我认为正在发生的事情,看看我是否走在正确的轨道上。似乎当应用程序第一次编译到我的手机上时,一切都很好(即主 Activity 中的全局变量可以正确看到并处理)。

但是,在查看 Pavel B. 建议我查看的链接后,似乎通过这个过程... A simplified illustration of the activity lifecycle.

...一旦我杀死应用程序并重新打开它,它就会直接进入 onCreate() 方法(并且似乎跳过了上面定义的全局变量)。这是怎么回事?

我已对我认为有帮助的评论进行了投票,但它表示由于我的声誉低于 15,因此不会公开。

再次非常感谢您!!

卢克

最佳答案

这可能会也可能不会解决您的问题,但是当您从主要 Activity 中调用“赌博” Activity 时,您应该使用 startActivityForResult() 并注册回调接收器来处理结果...

即您当前正在“启动”主要 Activity 本身,而不是仅通过 setResult 将结果传递回它

参见:

https://developer.android.com/training/basics/intents/result.html

https://developer.android.com/reference/android/os/ResultReceiver.html

更新:

考虑创建一个您可以使用的静态最终请求代码:

public static final int REQUEST_CODE_GAMBLE = 172;

使用静态请求代码将 getCoin 方法更改为 startActivityForResult():

public void getOneCoin(View view){
Intent intent = new Intent(this, gamble.class);
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("COIN_HISTORY", coinsOverTime);
intent.putExtra("GAMBLE", 1);
intent.putExtra("COINS", coins);
intent.putExtras(bundle);
startActivityForResult(intent, REQUEST_CODE_GAMBLE);
}

然后在 MainActivity 中重写 onActivityResult():

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
if(requestCode == REQUEST_CODE_GAMBLE){
if(resultCode == RESULT_OK){

//do all that same stuff you were doing in onCreate() in your MainActivity
//i.e. :
coins = intent.getIntExtra("COINS", 1);
//etc

}
}


}

然后在赌博 Activity 中的 Winner() 和 Loser() 方法中,只需设置结果并调用 finish():

public void winner()
{
Intent oldintent = getIntent();
Bundle oldbundle = oldintent.getExtras();
int gambleamount = oldintent.getIntExtra("GAMBLE", 1);
int coins = oldintent.getIntExtra("COINS", 1);
ArrayList<Integer> coinHistory = oldbundle.getIntegerArrayList("COIN_HISTORY");
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("COIN_HISTORY", coinHistory);
intent.putExtra("WINNER", true);
intent.putExtra("GAMBLE", gambleamount);
intent.putExtra("COINS", coins);
intent.putExtras(bundle);
getParent().setResult(Activity.RESULT_OK, intent);
finish();
}

当您的赌博 Activity “完成”时,它会将所有 Intent 数据发送到 MainActivity 的新 onActivityResult() 方法,您可以用它做您想做的事情

关于Android 应用程序重新打开后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43263978/

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