所以我有一个非常简单的主要活动为什么Android应用在startActivity上崩溃?
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void setDecks(View view){
EditText editTextCardNum = (EditText) findViewById(R.id.editText);
String CardNumString = editTextCardNum.getText().toString();
Intent intent = new Intent(this, Cards.class);
intent.putExtra("deckNumber", CardNumString);
startActivity(intent);
}
}
,我想用户输入一个数字到EDITTEXT,它在按下按钮将被传递到一个新的活动。这总是会导致应用程序崩溃。
我尝试注释掉的东西,看看它出了问题,它总是在
startActivity(intent)
失败,如果该行被注释掉,一切工作正常。新活动代码看起来像
public int totalCards = 0;
public int unknownCards = 0;
public int deckNumber = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cards);
Intent intent = getIntent();
String stringOfDecks = intent.getStringExtra("deck Number");
int numberOfDecks = Integer.parseInt(stringOfDecks);
totalCards = numberOfDecks * 52;
deckNumber = numberOfDecks;
unknownCards = totalCards;
//updateChance();
TextView summary = (TextView) findViewById(R.id.textView14);
summary.setText(Integer.toString(unknownCards));
}
以前,我曾试图通过意图传递数为int,而不是一个字符串,然后我的应用程序不会停止和崩溃,但事实证明我是做得不对,因为
int numberOfDecks = intent.getIntExtra("key", 0)
只是简单地将该值赋值为0?
编辑
我才意识到了问题的一部分,当我试图从tablerow的改变我的布局来约束可能已经。这是我的activity_cards.xml文件,我是否意外删除了必要的行?
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jaimevandeveer.cardcounter.Cards">
<Button
android:id="@+id/buttonA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:onClick="incrementA"
android:text="A"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="2"
android:onClick="increment2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonA" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="3"
android:onClick="increment3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="4"
android:onClick="increment4"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button3" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="6"
android:onClick="increment6"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button5" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="5"
android:onClick="increment5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button4" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="7"
android:onClick="increment7"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button6" />
<TextView
android:id="@+id/textViewA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/buttonA"
android:layout_marginLeft="16dp"
app:layout_constraintBaseline_toBaselineOf="@+id/buttonA" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/button2"
android:layout_marginLeft="16dp"
app:layout_constraintBaseline_toBaselineOf="@+id/button2" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/button3"
android:layout_marginLeft="16dp"
app:layout_constraintBaseline_toBaselineOf="@+id/button3" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/button4"
android:layout_marginLeft="16dp"
app:layout_constraintBaseline_toBaselineOf="@+id/button4" />
<TextView
android:id="@+id/textView5"
android:layout_width="56dp"
android:layout_height="31dp"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/button5"
android:layout_marginLeft="16dp"
app:layout_constraintBaseline_toBaselineOf="@+id/button5" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/button6"
android:layout_marginLeft="16dp"
app:layout_constraintBaseline_toBaselineOf="@+id/button6" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/button7"
android:layout_marginLeft="14dp"
app:layout_constraintBaseline_toBaselineOf="@+id/button7" />
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="8"
android:onClick="increment8"
app:layout_constraintLeft_toRightOf="@+id/textViewA"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="9"
android:onClick="increment9"
app:layout_constraintLeft_toRightOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/button8" />
<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="10"
android:onClick="increment10"
app:layout_constraintLeft_toRightOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/button12" />
<Button
android:id="@+id/buttonJ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="J"
android:onClick="incrementJ"
app:layout_constraintLeft_toRightOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/button10" />
<Button
android:id="@+id/buttonQ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Q"
android:onClick="incrementQ"
app:layout_constraintTop_toBottomOf="@+id/buttonJ"
app:layout_constraintLeft_toRightOf="@+id/textView5"
android:layout_marginLeft="16dp" />
<Button
android:id="@+id/buttonK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="K"
android:onClick="incrementK"
app:layout_constraintLeft_toRightOf="@+id/textView6"
app:layout_constraintTop_toBottomOf="@+id/buttonQ" />
<TextView
android:id="@+id/textView8"
android:layout_width="58dp"
android:layout_height="17dp"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/button8"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBaseline_toBaselineOf="@+id/button8" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/button12"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBaseline_toBaselineOf="@+id/button12" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/button10"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBaseline_toBaselineOf="@+id/button10" />
<TextView
android:id="@+id/textViewJ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/buttonJ"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBaseline_toBaselineOf="@+id/buttonJ" />
<TextView
android:id="@+id/textViewQ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/buttonQ"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBaseline_toBaselineOf="@+id/buttonQ" />
<TextView
android:id="@+id/textViewK"
android:layout_width="60dp"
android:layout_height="17dp"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/buttonK"
android:layout_marginLeft="16dp"
app:layout_constraintBaseline_toBaselineOf="@+id/buttonK"
android:layout_marginRight="16dp"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="@+id/textView14"
android:layout_width="72dp"
android:layout_height="32dp"
android:text="TextView"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/buttonK"
app:layout_constraintLeft_toRightOf="@+id/textView7"
android:layout_marginLeft="16dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp" />
</android.support.constraint.ConstraintLayout>
你在logcat的看到什么异常?你可以发布吗? –
post logcat谢谢 –
恩,对不起,我对此很陌生。什么是logcat?在android studio上,它编译好并在我的设备上启动它。然后当我点击按钮时,它说应用程序已停止 – JVandeve