在Android应用程序中使用显式Intent“无法找到显式Activity类”

cu6pst1q  于 2023-05-27  发布在  Android
关注(0)|答案(2)|浏览(189)

我是非常新的创建应用程序,我只是希望另一个活动显示,一旦一个按钮被按下。我在其他项目中这样做过,没有遇到任何问题,现在3天了,我似乎无法理解为什么我的按钮不起作用。我得到了“不幸的是,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)
4xrmg8kj

4xrmg8kj1#

你的代码看起来很好。另一个Activity是否在AndroidManifest.xml中?

EDIT通过查看错误日志,您的问题肯定是该类未在清单中声明。申报了就没事了。

7jmck4yq

7jmck4yq2#

试试这个。
在清单中的</application>之前添加下面一行

<activity
        android:name="com.esol.turtlemath.PythagoreanCalculator" />

相关问题