开始一项新活动

问题描述 投票:0回答:2

我对编程比较新,所以要温柔= |我正在尝试从显示一些文本输入,复选框和按钮的基本活动开始新活动。按下按钮时,我希望它切换到新活动。代码编译但是当我按下Android中的按钮时它只是崩溃。任何帮助将不胜感激。

这是代码示例:

public class Something extends Activity implements OnClickListener
{

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

       Button login = (Button)findViewById(R.id.login);
       login.setOnClickListener(this);

   }

   @Override
   public void onClick(View v) 
   {

       startActivity(new Intent().setClass(Something.this, That.class));
   }
}

错误日志:

I / ActivityManager(240):开始活动:Intent {cmp = jano.huerta.sfgc / .HOME} D / AndroidRuntime(13612):关闭VM W / dalvikvm(13612):threadid = 1:线程退出,未捕获异常( group = 0x4 0025a08)E / AndroidRuntime(13612):FATAL EXCEPTION:main E / AndroidRuntime(13612):java.lang.RuntimeException:无法启动活动Co mponentInfo {jano.huerta.sfgc / jano.huerta.sfgc.HOME} :android.content.ActivityNot FoundException:无法找到显式活动类{jano.huerta.sfgc / jano.hu erta.sfgc.SUMMARY};你有没有在AndroidManifest.xml中声明这个活动?

E / AndroidRuntime(13612):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)E / AndroidRuntime(13612):在android.app.ActivityThread.handleLaunchActivi ty(ActivityThread.java:2803)E / AndroidRuntime( 13612):在android.app.ActivityThread.access $ 2300(Activi tyThread.java:135)E / AndroidRuntime(13612):在android.app.ActivityThread $ H.handleMessage(Ac tivityThread.java:2136)E / AndroidRuntime(13612) ):在android.os.Handler.dispatchMessage(Handler.ja va:99)E / AndroidRuntime(13612):在android.os.Looper.loop(Looper.java:144)E / AndroidRuntime(13612): 在android.app.ActivityThread.main(ActivityThrea d.java:4937)E / AndroidRuntime(13612):at java.lang.reflect.Method.invokeNative(Native Method)E / AndroidRuntime(13612):at java.lang.reflect .Method.invoke(Method.java:5 21)E / AndroidRuntime(13612):at com.android.internal.os.ZygoteInit $ MethodAndA rgsCaller.run(ZygoteInit.java:868)E / AndroidRuntime(13612):at com .android.internal.os.ZygoteInit.main(Zygot eInit.java:626)E / AndroidRuntime(13612):at dalvik.system.NativeStart.main(Native Method)

E / AndroidRuntime(13612):引起:android.content.ActivityNotFoundException:无法找到显式活动类{jano.huerta.sfgc / jano.huerta.sfgc.SUMMARY};你有没有在AndroidManifest.xml中声明这个活动? E / AndroidRuntime(13612):在android.app.Instrumentation.checkStartActivit yResult(Instrumentation.java:1563)E / AndroidRuntime(13612):在android.app.ActivityThread.resolveActivityInf o(ActivityThread.java:2597)E / AndroidRuntime( 13612):在android.app.LocalActivityManager.startActivit y(LocalActivityManager.java:277)E / AndroidRuntime(13612):在android.widget.TabHost $ IntentContentStrategy。 getContentView(TabHost.java:651)E / AndroidRuntime(13612):在android.widget.TabHost.setCurrentTab(TabHost。java:323)E / AndroidRuntime(13612):在android.widget.TabHost.addTab(TabHost.java: 21 3)E / AndroidRuntime(13612):at jano.huerta.sfgc.HOME.onCreate(HOME.java:23)E / AndroidRuntime(13612):at android.app.Instrumentation.callActivityOnCre ate(Instrumentation.java:1069) E / AndroidRuntime(13612):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)E / AndroidRuntime(13612):... 11更多W / ActivityManager(240):强制完成活动jano.huerta.sfgc /.HOME W / ActivityManager(240):强制完成活动jano.huerta.sfgc / .SFGC W / ActivityManager(240):HistoryRecord的活动暂停超时{466bbec0 jano .huerta.sfgc / .HOME} D / dalvikvm(406) :GC_EXPLICIT在90ms I / Process(13612)中释放了418个对象/ 26008个字节:发送信号。 PID:13612 SIG:9 I / ActivityManager(240):进程jano.huerta.sfgc(pid 13612)已经死亡。 I / WindowManager(240):胜利死亡:窗口{466cb800 jano.huerta.sfgc / jano.huerta。 sfgc.SFGC paused = true} W / ActivityManager(240):HistoryRecord的活动销毁超时{46654450 ja no.huerta.sfgc / .SFGC}

android android-activity
2个回答
1
投票

这里最常见的错误是忘记在AndroidManifest.xml中注册'That'活动。你做到了吗?

此外,如果您从崩溃中粘贴堆栈跟踪,那么帮助您会更容易。您可以通过运行adb logcat来查看崩溃信息以及其他日志记录。


0
投票

Never forget to add the <activity> tag in the Manifest.xml.

所有活动必须添加到清单文件中。 Android Activity是一个必备的Android组件,应该注册。

Why?

活动由Android OS启动和销毁。所以当你打电话的时候

Intent i = new Intent(this, TargetActivity.class);
startActicvity(i);

然后操作系统在Manifest.xml文件中查找Activity。如果找不到匹配的活动,那么您将收到以下错误:

android.content.ActivityNot FoundException:无法找到显式活动

How to add activity in Manifest file?

在Manifest文件中添加它。

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

这是最基本的形式。您可以探索更多HERE

© www.soinside.com 2019 - 2024. All rights reserved.