gpt4 book ai didi

java - 如何编码 Activity 页面... android

转载 作者:行者123 更新时间:2023-11-30 03:06:50 26 4
gpt4 key购买 nike

我是 android 的新手,我正在学习教程。我想要做的是链接 2 个 xml 页面。登录后进入内容页面。问题是当我在我的类文件中添加 Intent 代码时,模拟器会提供错误消息“应用程序已停止工作”,否则代码工作正常。

这是我尝试过的 Intent 代码:

Intent myIntent = new Intent(v.getContext(), secondpage.class);
startActivityForResult(myIntent, 1);

这个我也试过

Intent myIntent = new Intent(MainActivity.this, secondpage.class);
startActivity(myIntent);

最后,这个

Intent myIntent = new Intent(getApplicationContext(), secondpage.class);
startActivity(myIntent);

我的secondpage.class代码如下(xml文件的名字是cf)

public class secondpage extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cf);
}
}

我做错了什么?为什么 Intent 会导致模拟器闪烁错误消息?

我的 manifest.xml

  <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.qwedd.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

我的日志猫

02-10 03:07:49.440: D/dalvikvm(903): Not late-enabling CheckJNI (already on)
02-10 03:07:52.290: D/dalvikvm(903): GC_FOR_ALLOC freed 54K, 5% free 2890K/3016K, paused 39ms, total 41ms
02-10 03:07:52.370: I/dalvikvm-heap(903): Grow heap (frag case) to 12.512MB for 10092616-byte allocation
02-10 03:07:52.440: D/dalvikvm(903): GC_FOR_ALLOC freed 2K, 2% free 12744K/12876K, paused 65ms, total 65ms
02-10 03:07:54.010: D/(903): HostConnection::get() New Host Connection established 0xb81424b8, tid 903
02-10 03:07:54.080: W/EGL_emulation(903): eglSurfaceAttrib not implemented
02-10 03:07:54.090: D/OpenGLRenderer(903): Enabling debug mode 0
02-10 03:08:00.020: D/InputEventConsistencyVerifier(903): KeyEvent: ACTION_UP but key was not down.
02-10 03:08:00.020: D/InputEventConsistencyVerifier(903): in android.widget.EditText{b3d9ea80 VFED..CL .F...... 15,315-465,374 #7f080001 app:id/txtPassword}
02-10 03:08:00.020: D/InputEventConsistencyVerifier(903): 0: sent at 103941000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=103941, downTime=103804, deviceId=0, source=0x101 }
02-10 03:08:38.556: D/AndroidRuntime(903): Shutting down VM
02-10 03:08:38.556: W/dalvikvm(903): threadid=1: thread exiting with uncaught exception (group=0xb3aeaba8)
02-10 03:08:38.566: E/AndroidRuntime(903): FATAL EXCEPTION: main
02-10 03:08:38.566: E/AndroidRuntime(903): Process: com.example.qwedd, PID: 903
02-10 03:08:38.566: E/AndroidRuntime(903): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.qwedd/com.example.qwedd.secondpage}; have you declared this activity in your AndroidManifest.xml?
02-10 03:08:38.566: E/AndroidRuntime(903): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
02-10 03:08:38.566: E/AndroidRuntime(903): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
02-10 03:08:38.566: E/AndroidRuntime(903): at android.app.Activity.startActivityForResult(Activity.java:3424)
02-10 03:08:38.566: E/AndroidRuntime(903): at android.app.Activity.startActivityForResult(Activity.java:3385)
02-10 03:08:38.566: E/AndroidRuntime(903): at android.app.Activity.startActivity(Activity.java:3627)
02-10 03:08:38.566: E/AndroidRuntime(903): at android.app.Activity.startActivity(Activity.java:3595)
02-10 03:08:38.566: E/AndroidRuntime(903): at com.example.qwedd.MainActivity$1.onClick(MainActivity.java:40)
02-10 03:08:38.566: E/AndroidRuntime(903): at android.view.View.performClick(View.java:4438)
02-10 03:08:38.566: E/AndroidRuntime(903): at android.view.View$PerformClick.run(View.java:18422)
02-10 03:08:38.566: E/AndroidRuntime(903): at android.os.Handler.handleCallback(Handler.java:733)
02-10 03:08:38.566: E/AndroidRuntime(903): at android.os.Handler.dispatchMessage(Handler.java:95)
02-10 03:08:38.566: E/AndroidRuntime(903): at android.os.Looper.loop(Looper.java:136)
02-10 03:08:38.566: E/AndroidRuntime(903): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-10 03:08:38.566: E/AndroidRuntime(903): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 03:08:38.566: E/AndroidRuntime(903): at java.lang.reflect.Method.invoke(Method.java:515)
02-10 03:08:38.566: E/AndroidRuntime(903): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-10 03:08:38.566: E/AndroidRuntime(903): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-10 03:08:38.566: E/AndroidRuntime(903): at dalvik.system.NativeStart.main(Native Method)
02-10 03:09:23.546: I/Process(903): Sending signal. PID: 903 SIG: 9

最佳答案

一般来说,每个 Activity 都需要它自己的 xml 布局文件(除非使用更高级的方法,无论如何这可能不是您的 Intent )。

在 onCreate 方法中,您会看到 setContentView(R.layout.cf); - 这意味着此 Activity 正在调用名为“cf”的布局 xml。

因此,如果您想要两项 Activity 并且想要从一项 Activity 转到另一项 Activity - 请执行以下操作:

1) 在第一个 Activity 中,在 onCreate 方法中,像这样调用您的第一个布局文件:

 public class firstActivity extends Activity{

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.firstlayout);

}

在您的第二个 Activity 中,在 onCreate 方法中,像这样调用您的第二个布局文件:

public class secondActivity extends Activity{

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.secondlayout);

}

如果您有多个 Activity ,则需要在您的 manifest.xml 中声明它们例如:

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

现在,在您的第一个 Activity 中,您通常会希望在按下按钮或其他操作时转到第二个 Activity。因此,在您的 onClickListener(或任何您想转到第二页的地方)中,您可以这样调用 Intent:

Intent intent = new Intent(firstPage.this, secondPage.class);
startActivity(intent);

这应该可以解决问题!

关于java - 如何编码 Activity 页面... android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21671838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com