2017-07-31 101 views
0

所以我有一个非常简单的主要活动为什么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> 
+0

你在logcat的看到什么异常?你可以发布吗? –

+0

post logcat谢谢 –

+0

恩,对不起,我对此很陌生。什么是logcat?在android studio上,它编译好并在我的设备上启动它。然后当我点击按钮时,它说应用程序已停止 – JVandeve

回答

0

请确保第二个参数Intent构造是活动类(CardsActivity等),而不是Java POJO(卡)。

Intent intent = new Intent(this, <ACTIVITY_CLASS_NAME>.class); 

更换<ACTIVITY_CLASS_NAME>与您希望您的应用上startActivity过渡活动类的名称。

请确保您声明了一个public static final String常数,用于保存数据和从Intent中检索数据的密钥。

public class MainActivity .... 
...... 
public static final String DECK_KEY = "deckNumber"; 
...... 
// when inserting data in intent 
intent.putExtra(MainActivity.DECK_KEY, "value"); 

...... 
// when retrieving data from intent 
String stringOfDecks = intent.getStringExtra(MainActivity.DECK_KEY); 

还要确保活动是在清单文件中的标签下标签下声明的。

+0

嗨,我通过右键单击应用程序并创建一个新的空闲活动来创建Cards.java。我是否应该将它命名为CardsActivity,而不是将其命名为活动课程? – JVandeve

+0

不,名称中的“活动”只是命名约定,易于读者阅读。如果Cards.java是一个活动,那么我们需要更多关于logcat异常的细节。 – JRG

+0

您是否还可以确认AndroidManifest xml是否使用活动标签声明了卡片? – JRG

0

命名为您布局文件

activity_cards

我必须假设你的代码需要以下修订从

意向意图=新意图(这一点, Cards.class);

意图意图=新意图(此,CardsActivity。类);

如果我的第一个假设是错误的,那么我认为你自己构建了活动和布局,因为它们没有遵循相同的命名约定。如果是这样的话,你可能已经忘记了活动添加到您的清单文件:

<activity android:name=".Cards" /> 

你的最终可能会看起来像这样:

<activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".Cards"> 
    </activity> 

你也有你的卡一个错字的onCreate方法。

String stringOfDecks = intent.getStringExtra("deck Number"); 

应该是:

String stringOfDecks = intent.getStringExtra("deckNumber"); 
+0

我的其他活动显示为Cards.java,然后将布局设置为activity_cards,但我的主要活动称为MainActivity,它与布局为activity_main。我需要更改Cards.java的名称吗?如果我用新的Intent(this,CardsActivity.class)替换代码,它会告诉我它无法解析符号 – JVandeve