我不仅是Android Studio的新手,我是一般的编程新手(我一直在慢慢地教我自己过去一年左右的爱好)。
我遇到了一个我的Android应用程序的问题,我无法找到答案 - 尽管我知道很多人都问过类似的问题,我似乎无法理解解决方案足以让它适应我的特别的问题。编译时,我的应用程序将正常运行并执行我想要“正确”执行的所有操作(除了工具栏上的小东西,但我稍后会介绍哈哈)。
在我关闭之后,它没有做的一件事是正确打开的。我可以转到主屏幕并返回应用程序,但是如果我完全关闭应用程序并尝试重新打开则说它“一直停止。”
现在,我认为给你我的代码(这个应用程序中只有两个活动)和我的“logcat”会很有用。但是,我现在正在尝试阅读并了解如何首先检索我的“logcat”哈哈,所以希望我很快就能在这里找到它。
从我的阅读中,我的理解是我的问题是我正在声明的全局变量或者当我动态制作按钮时按钮ID的问题。我遇到了与他们的“线程”有问题的人,但我无法将其与我的特定位置联系起来。但是,如果其中任何一个是问题,我很难理解问题究竟是什么。
这是我的主要活动......
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;
}
}
这是与之交谈的赌博活动......
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)
更新
首先,谢谢您的回复!这对我来说非常有教育意义!我想按照我现在的逻辑运行,看看我是否走在正确的轨道上。看起来当应用程序首次编译到我的手机上时,一切正常(即主要活动中的全局变量被正确显示和处理)。
然而,在看了Pavel B建议的链接之后我看,似乎通过这个过程...... A simplified illustration of the activity lifecycle.
...一旦我杀了应用程序并将其打开,它会直接进入onCreate()方法(并且似乎已跳过上面定义的全局变量)。这是怎么回事?
我已经对我发现有帮助的评论进行了投票,但是由于我的声誉低于15,因此不会公开。
再次感谢你!!
路
答案 0 :(得分:0)
这可能会或可能不会解决您的问题,但是当您打电话给您的赌博&#39;来自您的主要活动的活动,你应该使用startActivityForResult()并注册一个回调接收器来处理结果......
即。你现在正在开始&#39;你自己的主要活动,而不是通过setResult
将结果传回给它请参阅:
https://developer.android.com/training/basics/intents/result.html
和
https://developer.android.com/reference/android/os/ResultReceiver.html
<强>更新强>
考虑创建一个可以使用的静态最终请求代码:
Mono.Android
使用静态请求代码将getCoin方法更改为startActivityForResult():
public static final int REQUEST_CODE_GAMBLE = 172;
然后覆盖MainActivity中的onActivityResult():
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);
}
然后在赌博活动中的wins()和loser()方法中,只需设置结果并调用finish():
@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
}
}
}
当您的赌博活动结束时,它会将所有意图数据发送到您的MainActivity的新onActivityResult()方法,并且您可以使用它来做您喜欢的事情
答案 1 :(得分:0)
似乎错误是您的coinsOverTime数组为空。当你的活动以一个意图开始时,它可以很好地存储这个数组作为额外的存储,但当你的应用程序离开屏幕并返回时,情况并非如此。
当您按下主页时,您的活动应保存其状态并在屏幕上显示时将其恢复。 请在此处阅读保存和恢复活动状态部分: https://developer.android.com/guide/components/activities/activity-lifecycle.html
答案 2 :(得分:0)
只需将if(coinsOverTime != null)
放在coinsOverTime.add(coins);
之前的每个地方。发生此崩溃是因为在恢复活动时coinsOverTime
数组为空指针。