所以我有一個非常簡單的主要活動爲什麼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