使用明确的意图,Android应用程序

时间:2014-04-11 16:05:31

标签: java android android-intent

我是创建应用程序的新手,我只想在按下按钮后再显示其他活动。我已经在其他项目中完成了这项工作而没有任何问题,现在我似乎无法理解为什么我的按钮不起作用。我得到了#34;不幸的是,TurtleMath已经停止了。" TurtleMath是应用程序的名称。我认为这是一个非常愚蠢的错误或语法错误,但我似乎无法找到它。非常感谢任何帮助,谢谢。我发布了PCalculator.java和PythagoreanCalculator.java。按钮是斜边和侧面,我希望它们打开PythagoreanCalculator.java。如果需要XML文件,请告诉我,我会将它们添加进来。

public class PCalulator extends Activity {

private Button mSideButton;
private Button mHypotenuseButton;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pcalulator);

    mSideButton = (Button)findViewById(R.id.side_button);
    mSideButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Intent i = new Intent(PCalulator.this, PythagoreanCalculator.class);
            startActivity(i);
        }
    });
    mHypotenuseButton = (Button)findViewById(R.id.hypotenuse_button);
    mHypotenuseButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
             Intent i = new Intent(PCalulator.this, PythagoreanCalculator.class);
             startActivity(i);
        }
    });
}

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

}

public class PythagoreanCalculator extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pythagorean_calculator);
}
}

    04-11 12:14:45.950: E/AndroidRuntime(27272): FATAL EXCEPTION: main
04-11 12:14:45.950: E/AndroidRuntime(27272): Process: com.esol.turtlemath, PID: 27272
04-11 12:14:45.950: E/AndroidRuntime(27272): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.esol.turtlemath/com.esol.turtlemath.PythagoreanCalculator}; have you declared this activity in your AndroidManifest.xml?
04-11 12:14:45.950: E/AndroidRuntime(27272):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at android.app.Activity.startActivityForResult(Activity.java:3468)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at android.app.Activity.startActivityForResult(Activity.java:3429)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at android.app.Activity.startActivity(Activity.java:3671)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at android.app.Activity.startActivity(Activity.java:3639)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at com.esol.turtlemath.PCalulator$2.onClick(PCalulator.java:38)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at android.view.View.performClick(View.java:4442)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at android.view.View$PerformClick.run(View.java:18473)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at android.os.Handler.handleCallback(Handler.java:733)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at android.os.Handler.dispatchMessage(Handler.java:95)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at android.os.Looper.loop(Looper.java:136)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at android.app.ActivityThread.main(ActivityThread.java:5105)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at java.lang.reflect.Method.invokeNative(Native Method)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at java.lang.reflect.Method.invoke(Method.java:515)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
04-11 12:14:45.950: E/AndroidRuntime(27272):    at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

你的代码看起来很好。是AndroidManifest.xml中的其他活动吗?

编辑您的问题肯定是通过查看错误日志未在清单中声明该类。宣布它,你应该没事。

答案 1 :(得分:0)

试试这个..

</application>

之前在清单中添加以下行
  <activity
        android:name="com.esol.turtlemath.PythagoreanCalculator" />