您好我正在开发一个用于教育的启动器,我有一个计算器活动和xml布局文件,我想要包含在fragmenttab2中我在xml中使用包含布局,它显示正常,但是当我单击一个按钮时说数字3应用程序崩溃我做错了什么?
fragmenttab2.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg" >
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
layout="@layout/activity_calculator" />
<Button
android:id="@+id/app"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/allapps" />
<Button
android:id="@+id/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/tools" />
<Button
android:id="@+id/web"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/internet" />
</RelativeLayout>
计算器按钮:
button0=(Button)findViewById(R.id.button0);
button1=(Button)findViewById(R.id.engery);
button2=(Button)findViewById(R.id.heatcal);
button3=(Button)findViewById(R.id.tempcal);
button4=(Button)findViewById(R.id.button4);
button5=(Button)findViewById(R.id.button5);
button6=(Button)findViewById(R.id.button6);
button7=(Button)findViewById(R.id.button7);
button8=(Button)findViewById(R.id.button8);
button9=(Button)findViewById(R.id.button9);
buttonPlus=(Button)findViewById(R.id.buttonPlus);
buttonMinus=(Button)findViewById(R.id.buttonMinus);
buttonMultiply=(Button)findViewById(R.id.buttonMultiply);
buttonDivide=(Button)findViewById(R.id.buttonDivide);
buttonPoint=(Button)findViewById(R.id.buttonPoint);
buttonEqual=(Button)findViewById(R.id.buttonEqual);
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
//button_del=(Button)findViewById(R.id.button_del)
logcat的:
09-22 00:18:53.036: D/AndroidRuntime(1578): Shutting down VM
09-22 00:18:53.036: W/dalvikvm(1578): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-22 00:18:53.076: E/AndroidRuntime(1578): FATAL EXCEPTION: main
09-22 00:18:53.076: E/AndroidRuntime(1578): java.lang.IllegalStateException: Could not find a method onClickListener7(View) in the activity class com.d4a.stzh.MainActivity for onClick handler on view class android.widget.Button with id 'button7'
09-22 00:18:53.076: E/AndroidRuntime(1578): at android.view.View$1.onClick(View.java:3586)
09-22 00:18:53.076: E/AndroidRuntime(1578): at android.view.View.performClick(View.java:4204)
09-22 00:18:53.076: E/AndroidRuntime(1578): at android.view.View$PerformClick.run(View.java:17355)
09-22 00:18:53.076: E/AndroidRuntime(1578): at android.os.Handler.handleCallback(Handler.java:725)
09-22 00:18:53.076: E/AndroidRuntime(1578): at android.os.Handler.dispatchMessage(Handler.java:92)
09-22 00:18:53.076: E/AndroidRuntime(1578): at android.os.Looper.loop(Looper.java:137)
09-22 00:18:53.076: E/AndroidRuntime(1578): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-22 00:18:53.076: E/AndroidRuntime(1578): at java.lang.reflect.Method.invokeNative(Native Method)
09-22 00:18:53.076: E/AndroidRuntime(1578): at java.lang.reflect.Method.invoke(Method.java:511)
09-22 00:18:53.076: E/AndroidRuntime(1578): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-22 00:18:53.076: E/AndroidRuntime(1578): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-22 00:18:53.076: E/AndroidRuntime(1578): at dalvik.system.NativeStart.main(Native Method)
09-22 00:18:53.076: E/AndroidRuntime(1578): Caused by: java.lang.NoSuchMethodException: onClickListener7 [class android.view.View]
09-22 00:18:53.076: E/AndroidRuntime(1578): at java.lang.Class.getConstructorOrMethod(Class.java:460)
09-22 00:18:53.076: E/AndroidRuntime(1578): at java.lang.Class.getMethod(Class.java:915)
09-22 00:18:53.076: E/AndroidRuntime(1578): at android.view.View$1.onClick(View.java:3579)
09-22 00:18:53.076: E/AndroidRuntime(1578):
任何帮助都会令人惊讶我仍然是新手,所以请不要判断
Rapsong11
答案 0 :(得分:0)
是关于你的“button7.setOnClickListener()
”。你可以检查你的button7定义,像这样;
button_name = (Button) findviewbyId(R.id.button7);
你应该在定义后添加clicklistener。
顺便说一句,如果你想定义&amp;使用您活动中的任何视图,首先应加载所需的xml文件。使用“setContentView();
”
这只是一个假设,因为我需要查看您的代码。
也许this可以帮到你。