尝试切换到新活动时,应用程序崩溃

时间:2016-01-29 15:42:42

标签: android android-intent android-activity

这是我关于stackoverflow的第一个问题,所以不要判断:: D 我正在创建一个简单的Android应用程序,它是关于物理的(不要问为什么,因为我不知道答案)。所以我创建了3个活动。主要布局组成使得有2个按钮。一个是进入有3个牛顿运动定律的活动,另一个是进入有热力学定律的活动。现在我已经设法从Main活动切换到Newton活动(我在youtube上观看了一个教程)但是当我尝试用热力学做同样的事情时(代码完全相同)但是当我点击按钮(热力学按钮)时应用程序崩溃。这是代码:

MainActivity

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    else if (id == R.id.action_exit){
        finish();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void onGetNewtonLaw(View view)
{
    Intent getNewtonLawIntent = new Intent(this, NewtonScreen.class);

    final int result = 1;

    startActivityForResult(getNewtonLawIntent,result);
}

public void onGetThermodynamicsLaw(View view)
{
    Intent getThermodynamicsLawIntent = new Intent(this, ThermodynamicsScreen.class);

    final int result = 1;

    startActivityForResult(getThermodynamicsLawIntent,result);

}

}

热力学屏幕活动

public class ThermodynamicsScreen extends Activity {

 private Button thermodynamicsLawOneButton,  thermodynamicsLawTwoButton,thermodynamicsLawThreeButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.thermodynamics_layout);

    thermodynamicsLawOneButton = (Button) findViewById(R.id.thermodynamics_law_1_button);
    thermodynamicsLawTwoButton = (Button) findViewById(R.id.thermodynamics_law_2_button);
    thermodynamicsLawThreeButton = (Button) findViewById(R.id.thermodynamics_law_3_button);
}

public  void onClickThermodynamicsLawOne(View view)
{
    Toast.makeText(this, "Energy can neither be created nor destroyed; it can only be transferred or changed from one form to another.\n\n ΔE=q+w ",Toast.LENGTH_LONG).show();
}
public void onClickThermodynamicsLawTwo(View view)
{
    Toast.makeText(this, "The entropy of the world only increases and never decreases.", Toast.LENGTH_LONG).show();
}
public void onClickThermodynamicsLawThree(View view)
{
    Toast.makeText(this, "that the entropy of a system approaches a constant value as the temperature approaches absolute zero.", Toast.LENGTH_LONG).show();
}
public void onClickBackToMainMenu (View view)
{
    Intent mainMenu = new Intent();

    setResult(RESULT_OK, mainMenu);

    finish();
}

}

NewtonScreen活动

public class NewtonScreen extends Activity
{


private Button theAnswerButton, theHintButton, theSuckButton;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.newton_layout);

    theAnswerButton = (Button) findViewById(R.id.answer_button);
    theHintButton = (Button) findViewById(R.id.hint_button);
    theSuckButton = (Button)findViewById(R.id.suck_at_physics_button);

}

public void onClickTheAnswerButton(View view)
{

    Toast.makeText(this,
            "An object at rest will remain at rest unless acted on by an unbalanced force. " +
                    "An object in motion continues in motion with the same speed and in the same direction" +
                    " unless acted upon by an unbalanced force.",
            Toast.LENGTH_LONG).show();

}

public  void onClickTheHintButton(View view)
{
    Toast.makeText(this, "F = m*a",Toast.LENGTH_SHORT).show();
}

public  void onClickTheSuckPhysicsButton(View view)
{
    Toast.makeText(this, "For every action there is an equal and opposite re-action.", Toast.LENGTH_LONG).show();
}

public void onClickBackToMainMenu (View view)
{
    Intent mainMenu = new Intent();

    setResult(RESULT_OK, mainMenu);

    finish();
}

}

E / AndroidRuntime:FATAL EXCEPTION:main

记录(红色的一切)

       Process: com.example.android.newtonsfirstlawofmotion, PID: 8432
       java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3827)
        at android.view.View.performClick(View.java:4442)
        at android.view.View$PerformClick.run(View.java:18473)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5105)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at android.view.View$1.onClick(View.java:3822)

at android.view.View.performClick(View.java:4442)
            at android.view.View$PerformClick.run(View.java:18473)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5105)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.android.newtonsfirstlawofmotion/com.example.android.newtonsfirstlawofmotion.ThermodynamicsScreen}; have you declared this activity in your AndroidManifest.xml?
            at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
            at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
            at android.app.Activity.startActivityForResult(Activity.java:3468)
            at android.app.Activity.startActivityForResult(Activity.java:3429)
            at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:748)
            at com.example.android.newtonsfirstlawofmotion.MainActivity.onGetThermodynamicsLaw(MainActivity.java:63)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3822)
            at android.view.View.performClick(View.java:4442)
            at android.view.View$PerformClick.run(View.java:18473)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5105)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
            at dalvik.system.NativeStart.main(Native Method)

感谢您的回复! :)

3 个答案:

答案 0 :(得分:1)

错误的关键是这一行:

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.android.newtonsfirstlawofmotion/com.example.android.newtonsfirstlawofmotion.ThermodynamicsScreen}; have you declared this activity in your AndroidManifest.xml?

您似乎忘记在AndroidManifest.xml文件中声明ThermodynamicsScreen活动。打开该文件并声明您的ThermodynamicsScreen活动。它看起来应该类似于:

<activity
    android:name=".ThermodynamicsScreen"
    android:label="@string/your_title_string"
    android:parentActivityName=".MainActivity"
    android:theme="@style/AppTheme.NoActionBar">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".MainActivity" />
</activity>

答案 1 :(得分:0)

这一行是问题所在:

   Intent mainMenu = new Intent();

将所需参数传递给

Intent
像你一样的

构造函数在其他两个类中完成。

应该是

  Intent mainMenu = new Intent(this, NewtonScreen.class);

答案 2 :(得分:0)

谢谢大家,我在搜索日志时发现了自己的错误(如果你不推荐我,我就找不到它)。问题是我没有在清单中宣布热力学活动。它现在正在运作。没有比这个hahaha更好的感觉了:))