带有按钮的 Eclipse android 主菜单启动新活动(IES)
Posted
技术标签:
【中文标题】带有按钮的 Eclipse android 主菜单启动新活动(IES)【英文标题】:eclipse android main menu with button to start new activity(ies) 【发布时间】:2015-04-10 04:21:05 【问题描述】:我希望有一个带有按钮的主菜单,单击时每个按钮都会转到自己的选项卡。请更正我所做的代码,如下:
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
public void produkdanpromobutton(View view)
Intent Intentprodukpromo = new Intent(this , Produkpromotest.class);
startActivity(Intentprodukpromo);
public void simulasilist(View view)
Intent Intentsimulasilist = new Intent(this , Simulasilistcode.class);
startActivity(Intentsimulasilist);
public void aboutus(View view)
Intent Intentaboutus = new Intent(this , Aboutuscode.class);
startActivity(Intentaboutus);
我已经在清单和 xml 布局上添加了活动,但是 它不起作用,当我在 eclipse 模拟器(android 5554 模拟器)上尝试时,当我单击其中一个按钮时,应用程序将崩溃。
Produkpromotest.java
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.productpromotab);
Simulatorlistcode.java
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
Simulasilistcode.java 发现布局错误: 正确的:
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.simulasilisttab);
关于代码.java
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.abouttab);
mainmenu.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:orientation="vertical" >
<Button
android:layout_
android:layout_
android:layout_gravity="center"
android:layout_marginTop="90sp"
android:id="@+id/bprodukdanpromo"
android:onClick="produkdanpromobutton"
android:text="@string/produkpromo"
android:textSize="12sp"
/>
<Button
android:layout_
android:layout_
android:layout_gravity="center"
android:layout_marginTop="30sp"
android:id="@+id/bsimulasilist"
android:onClick="simulasilist"
android:text="@string/simulasilist"
android:textSize="12sp"
/>
<Button
android:layout_
android:layout_
android:layout_gravity="center"
android:layout_marginTop="30sp"
android:id="@+id/babout"
android:onClick="aboutus"
android:text="@string/about"
android:textSize="12sp"
/>
</LinearLayout>
没有错误,程序运行正常,但是当我单击按钮时它会崩溃。我一定是某个地方出错了,但我不知道在哪里。请帮我解决这个问题
提前致谢, 乔治
编辑:添加logcat
02-10 15:08:33.255: E/AndroidRuntime(371): FATAL EXCEPTION: main
02-10 15:08:33.255: E/AndroidRuntime(371): java.lang.IllegalStateException: Could not execute method of the activity
02-10 15:08:33.255: E/AndroidRuntime(371): at android.view.View$1.onClick(View.java:2072)
02-10 15:08:33.255: E/AndroidRuntime(371): at android.view.View.performClick(View.java:2408)
02-10 15:08:33.255: E/AndroidRuntime(371): at android.view.View$PerformClick.run(View.java:8816)
02-10 15:08:33.255: E/AndroidRuntime(371): at android.os.Handler.handleCallback(Handler.java:587)
02-10 15:08:33.255: E/AndroidRuntime(371): at android.os.Handler.dispatchMessage(Handler.java:92)
02-10 15:08:33.255: E/AndroidRuntime(371): at android.os.Looper.loop(Looper.java:123)
02-10 15:08:33.255: E/AndroidRuntime(371): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-10 15:08:33.255: E/AndroidRuntime(371): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 15:08:33.255: E/AndroidRuntime(371): at java.lang.reflect.Method.invoke(Method.java:521)
02-10 15:08:33.255: E/AndroidRuntime(371): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-10 15:08:33.255: E/AndroidRuntime(371): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-10 15:08:33.255: E/AndroidRuntime(371): at dalvik.system.NativeStart.main(Native Method)
02-10 15:08:33.255: E/AndroidRuntime(371): Caused by: java.lang.reflect.InvocationTargetException
02-10 15:08:33.255: E/AndroidRuntime(371): at com.m4nd1r1tun45f1n4nc3.abcdefg.Mainmenucode.produkdanpromobutton(Mainmenucode.java:18)
02-10 15:08:33.255: E/AndroidRuntime(371): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 15:08:33.255: E/AndroidRuntime(371): at java.lang.reflect.Method.invoke(Method.java:521)
02-10 15:08:33.255: E/AndroidRuntime(371): at android.view.View$1.onClick(View.java:2067)
02-10 15:08:33.255: E/AndroidRuntime(371): ... 11 more
02-10 15:08:33.255: E/AndroidRuntime(371): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class com.m4nd1r1tun45f1n4nc3.abcdefg/com.m4nd1r1tun45f1n4nc3.abcdefg.Produkpromotest; have you declared this activity in your AndroidManifest.xml?
02-10 15:08:33.255: E/AndroidRuntime(371): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
添加了 AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.m4nd1r1tun45f1n4nc3.abcdefg"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Loadingawalawal"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Mainmenucode"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.m4nd1r1tun45f1n4nc3.abcdefg.MAINMENUCODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Mastercode"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.m4nd1r1tun45f1n4nc3.abcdefg.MASTERCODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Productpromotest"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.m4nd1r1tun45f1n4nc3.abcdefg.PRODUCTPROMOTEST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Simulasilistcode"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.m4nd1r1tun45f1n4nc3.abcdefg.SIMULASILISTCODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Aboutuscode"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.m4nd1r1tun45f1n4nc3.abcdefg.ABOUTUSCODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Android Manifest 已编辑:将 Productpromotest 固定为 Produkpromotest, Produkpromo 测试正在运行,但另一个仍在崩溃。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.m4nd1r1tun45f1n4nc3.abcdefg"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Loadingawalawal"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Mainmenucode"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.m4nd1r1tun45f1n4nc3.abcdefg.MAINMENUCODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Mastercode"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.m4nd1r1tun45f1n4nc3.abcdefg.MASTERCODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Produkpromotest"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.m4nd1r1tun45f1n4nc3.abcdefg.PRODUKPROMOTEST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Simulasilistcode"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.m4nd1r1tun45f1n4nc3.abcdefg.SIMULASILISTCODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Aboutuscode"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.m4nd1r1tun45f1n4nc3.abcdefg.ABOUTUSCODE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
【问题讨论】:
when i click the button it will crash
请显示logcat结果
请发布崩溃!
我添加了它,我也是 eclipse 的新手,如何一次在 logcat 中获取所有日志(我必须一个一个地保存它并组合句子)。一些日志丢失了,就像那样。 ...11更多。谢谢,
【参考方案1】:
似乎活动未正确添加到清单中,或者拼写错误。请发布清单。
您正在调用 Produkpromotest.class,但它在清单中有所不同。
<activity
android:name=".Productpromotest" //RIGHT HERE
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.m4nd1r1tun45f1n4nc3.abcdefg.PRODUCTPROMOTEST" /> //and here
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
试试
<activity
android:name=".Produkpromotest" //RIGHT HERE
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.m4nd1r1tun45f1n4nc3.abcdefg.Produkpromotest" /> //and here
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
【讨论】:
是的,谢谢,Produkpromotest 对 Productpromotest 的拼写错误。 “k”和“ct”。谢谢 thxxx 它现在可以工作,但只有 Produkpromotest。其他的仍然导致崩溃。我认为这不是错误的拼写,知道吗? 您将第二个类命名为“Simulasilistcode”还是 Simulatorlistcode.java? 不,它的 Simulasilistcode,我刚刚发现我的布局参考战争错误。谢谢, 是的,所有按钮都可以工作,点击后它们会开始新的活动。我需要对变量更加细致,你提醒了我。谢谢以上是关于带有按钮的 Eclipse android 主菜单启动新活动(IES)的主要内容,如果未能解决你的问题,请参考以下文章
带有ActionBarDrawerToggle的Android菜单