当我运行应用程序时没有检测到错误,但是当我点击我的按钮时,应用程序崩溃了。我是Java和Android编程的新手。 android:onClick方法错了吗?
我的XML代码:
<?xml version="1.0" encoding="utf-8"?>
<!-- This is BMI page screen -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/numberPickerage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<!-- text for weight -->
<TextView
android:id="@+id/textweight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textage"
android:layout_below="@+id/textage"
android:layout_marginTop="21dp"
android:labelFor="@+id/weight"
android:text="@string/bmiweight"
android:textColor="#000000"
android:textSize="22sp"
android:typeface="sans" />
<!-- text for height -->
<TextView
android:id="@+id/textheight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textweight"
android:layout_below="@+id/textweight"
android:layout_marginTop="19dp"
android:labelFor="@+id/height"
android:text="@string/bmiheight"
android:textColor="#000000"
android:textSize="22sp"
android:typeface="sans" />
<!-- text for age -->
<TextView
android:id="@+id/textage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/texttitle"
android:layout_marginLeft="26dp"
android:layout_marginTop="33dp"
android:labelFor="@+id/editage"
android:text="@string/bmiage"
android:textColor="#000000"
android:textSize="22sp"
android:typeface="sans" />
<EditText
android:id="@+id/editage"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textage"
android:layout_alignBottom="@+id/textage"
android:layout_alignRight="@+id/textweight"
android:ems="10"
android:inputType="numberSigned" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/weight"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textweight"
android:layout_alignBottom="@+id/textweight"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/textheight"
android:ems="10"
android:inputType="numberDecimal" />
<EditText
android:id="@+id/height"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textheight"
android:layout_alignBottom="@+id/textheight"
android:layout_alignLeft="@+id/weight"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="@+id/btnBMIc"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textheight"
android:layout_below="@+id/height"
android:layout_marginTop="50dp"
android:text="@string/bmical"
android:onClick="calculateBMI" />
<!-- text for show bmi -->
<TextView
android:id="@+id/showBMI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/weight"
android:layout_alignRight="@+id/texttitle"
android:layout_alignTop="@+id/textage"
android:text="@string/defaultBMI"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="80sp" />
<!-- text for title -->
<TextView
android:id="@+id/texttitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:text="@string/bmihome"
android:textColor="#000000"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="sans" />
</RelativeLayout>
My Fragment Java代码:
package com.our.foodapp;
import com.our.foodapp.R;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
@SuppressLint("NewApi")
public class GP_Fragment extends Fragment {
EditText weights;
EditText heights;
Button btnBMI;
TextView bmiChg;
public double wei = 0;
public double hei = 0;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater .inflate(R.layout.gp_fragment, container, false);
//to calculate BMI
weights = (EditText)rootView.findViewById(R.id.weight);
heights = (EditText)rootView.findViewById(R.id.height);
btnBMI = (Button)rootView.findViewById(R.id.btnBMIc);
bmiChg = (TextView)rootView.findViewById(R.id.showBMI);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initializeApp();
}
void initializeApp()
{
weights = (EditText)getView().findViewById(R.id.weight);
heights = (EditText)getView().findViewById(R.id.height);
btnBMI = (Button)getView().findViewById(R.id.btnBMIc);
bmiChg = (TextView)getView().findViewById(R.id.showBMI);
}
@SuppressLint("DefaultLocale")
public void calculateBMI(View v)
{
wei = Double.parseDouble(weights.getText().toString() );
hei = Double.parseDouble(heights.getText().toString() );
double bmi = (wei/(hei * hei))* 703.0;
String result = String.format("%.2f",bmi);
//Change the default 0
bmiChg.setText(result, TextView.BufferType.NORMAL);
}
}
07-04 02:26:10.911: W/dalvikvm(26593): No implementation found for native Ldalvik/system/VMRuntime;.pauseGc:(Ljava/lang/String;)I
07-04 02:26:10.911: E/ActivityThread(26593): Pause GC
07-04 02:26:10.911: E/ActivityThread(26593): java.lang.reflect.InvocationTargetException
07-04 02:26:10.911: E/ActivityThread(26593): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 02:26:10.911: E/ActivityThread(26593): at java.lang.reflect.Method.invoke(Method.java:515)
07-04 02:26:10.911: E/ActivityThread(26593): at android.app.ActivityThread.pauseGC(ActivityThread.java:5525)
07-04 02:26:10.911: E/ActivityThread(26593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
07-04 02:26:10.911: E/ActivityThread(26593): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
07-04 02:26:10.911: E/ActivityThread(26593): at android.app.ActivityThread.access$900(ActivityThread.java:175)
07-04 02:26:10.911: E/ActivityThread(26593): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
07-04 02:26:10.911: E/ActivityThread(26593): at android.os.Handler.dispatchMessage(Handler.java:102)
07-04 02:26:10.911: E/ActivityThread(26593): at android.os.Looper.loop(Looper.java:146)
07-04 02:26:10.911: E/ActivityThread(26593): at android.app.ActivityThread.main(ActivityThread.java:5602)
07-04 02:26:10.911: E/ActivityThread(26593): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 02:26:10.911: E/ActivityThread(26593): at java.lang.reflect.Method.invoke(Method.java:515)
07-04 02:26:10.911: E/ActivityThread(26593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
07-04 02:26:10.911: E/ActivityThread(26593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
07-04 02:26:10.911: E/ActivityThread(26593): at dalvik.system.NativeStart.main(Native Method)
07-04 02:26:10.911: E/ActivityThread(26593): Caused by: java.lang.UnsatisfiedLinkError: Native method not found: dalvik.system.VMRuntime.pauseGc:(Ljava/lang/String;)I
07-04 02:26:10.911: E/ActivityThread(26593): at dalvik.system.VMRuntime.pauseGc(Native Method)
07-04 02:26:10.911: E/ActivityThread(26593): ... 15 more
07-04 02:26:11.046: D/dalvikvm(26593): GC_FOR_ALLOC freed 83K, 11% free 8299K/9240K, paused 20ms, total 20ms
07-04 02:26:11.056: I/dalvikvm-heap(26593): Grow heap (frag case) to 15.192MB for 6554896-byte allocation
07-04 02:26:11.076: D/dalvikvm(26593): GC_FOR_ALLOC freed <1K, 7% free 14700K/15644K, paused 20ms, total 20ms
07-04 02:26:11.136: D/libEGL(26593): loaded /system/lib/egl/libEGL_mali.so
07-04 02:26:11.141: D/libEGL(26593): loaded /system/lib/egl/libGLESv1_CM_mali.so
07-04 02:26:11.146: D/libEGL(26593): loaded /system/lib/egl/libGLESv2_mali.so
07-04 02:26:11.151: E/(26593): Device driver API match
07-04 02:26:11.151: E/(26593): Device driver API version: 23
07-04 02:26:11.151: E/(26593): User space API version: 23
07-04 02:26:11.151: E/(26593): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Fri Mar 21 13:52:50 KST 2014
07-04 02:26:11.241: D/OpenGLRenderer(26593): Enabling debug mode 0
07-04 02:26:11.346: W/dalvikvm(26593): No implementation found for native Ldalvik/system/VMRuntime;.pauseGc:(Ljava/lang/String;)I
07-04 02:26:11.351: E/ActivityThread(26593): Pause GC
07-04 02:26:11.351: E/ActivityThread(26593): java.lang.reflect.InvocationTargetException
07-04 02:26:11.351: E/ActivityThread(26593): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 02:26:11.351: E/ActivityThread(26593): at java.lang.reflect.Method.invoke(Method.java:515)
07-04 02:26:11.351: E/ActivityThread(26593): at android.app.ActivityThread.pauseGC(ActivityThread.java:5525)
07-04 02:26:11.351: E/ActivityThread(26593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2324)
07-04 02:26:11.351: E/ActivityThread(26593): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
07-04 02:26:11.351: E/ActivityThread(26593): at android.app.ActivityThread.access$900(ActivityThread.java:175)
07-04 02:26:11.351: E/ActivityThread(26593): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
07-04 02:26:11.351: E/ActivityThread(26593): at android.os.Handler.dispatchMessage(Handler.java:102)
07-04 02:26:11.351: E/ActivityThread(26593): at android.os.Looper.loop(Looper.java:146)
07-04 02:26:11.351: E/ActivityThread(26593): at android.app.ActivityThread.main(ActivityThread.java:5602)
07-04 02:26:11.351: E/ActivityThread(26593): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 02:26:11.351: E/ActivityThread(26593): at java.lang.reflect.Method.invoke(Method.java:515)
07-04 02:26:11.351: E/ActivityThread(26593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
07-04 02:26:11.351: E/ActivityThread(26593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
07-04 02:26:11.351: E/ActivityThread(26593): at dalvik.system.NativeStart.main(Native Method)
07-04 02:26:11.351: E/ActivityThread(26593): Caused by: java.lang.UnsatisfiedLinkError: Native method not found: dalvik.system.VMRuntime.pauseGc:(Ljava/lang/String;)I
07-04 02:26:11.351: E/ActivityThread(26593): at dalvik.system.VMRuntime.pauseGc(Native Method)
07-04 02:26:11.351: E/ActivityThread(26593): ... 15 more
07-04 02:26:11.466: D/dalvikvm(26593): GC_FOR_ALLOC freed 88K, 6% free 14829K/15776K, paused 13ms, total 13ms
07-04 02:26:11.476: I/dalvikvm-heap(26593): Grow heap (frag case) to 21.569MB for 6554896-byte allocation
07-04 02:26:11.491: D/dalvikvm(26593): GC_FOR_ALLOC freed <1K, 5% free 21230K/22180K, paused 14ms, total 14ms
07-04 02:26:11.516: D/AbsListView(26593): Get MotionRecognitionManager
07-04 02:26:11.556: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:11.561: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:11.561: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:11.561: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:11.661: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:11.671: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:11.676: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:11.746: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:11.746: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:11.746: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:11.751: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.041: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.046: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.046: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.431: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.436: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.656: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.661: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.716: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.716: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.716: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.716: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.721: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.721: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.731: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.731: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.731: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.736: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.736: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:14.736: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:16.891: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:17.096: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:18.131: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:18.336: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:18.611: D/TextLayoutCache(26593): Enable myanmar Zawgyi converter
07-04 02:26:19.436: D/AndroidRuntime(26593): Shutting down VM
07-04 02:26:19.436: W/dalvikvm(26593): threadid=1: thread exiting with uncaught exception (group=0x41c5bc08)
07-04 02:26:19.436: E/AndroidRuntime(26593): FATAL EXCEPTION: main
07-04 02:26:19.436: E/AndroidRuntime(26593): Process: com.our.foodapp, PID: 26593
07-04 02:26:19.436: E/AndroidRuntime(26593): java.lang.IllegalStateException: Could not find a method calculateBMI(View) in the activity class com.our.foodapp.Ourfood for onClick handler on view class android.widget.Button with id 'btnBMIc'
07-04 02:26:19.436: E/AndroidRuntime(26593): at android.view.View$1.onClick(View.java:3970)
07-04 02:26:19.436: E/AndroidRuntime(26593): at android.view.View.performClick(View.java:4654)
07-04 02:26:19.436: E/AndroidRuntime(26593): at android.view.View$PerformClick.run(View.java:19438)
07-04 02:26:19.436: E/AndroidRuntime(26593): at android.os.Handler.handleCallback(Handler.java:733)
07-04 02:26:19.436: E/AndroidRuntime(26593): at android.os.Handler.dispatchMessage(Handler.java:95)
07-04 02:26:19.436: E/AndroidRuntime(26593): at android.os.Looper.loop(Looper.java:146)
07-04 02:26:19.436: E/AndroidRuntime(26593): at android.app.ActivityThread.main(ActivityThread.java:5602)
07-04 02:26:19.436: E/AndroidRuntime(26593): at java.lang.reflect.Method.invokeNative(Native Method)
07-04 02:26:19.436: E/AndroidRuntime(26593): at java.lang.reflect.Method.invoke(Method.java:515)
07-04 02:26:19.436: E/AndroidRuntime(26593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
07-04 02:26:19.436: E/AndroidRuntime(26593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
07-04 02:26:19.436: E/AndroidRuntime(26593): at dalvik.system.NativeStart.main(Native Method)
07-04 02:26:19.436: E/AndroidRuntime(26593): Caused by: java.lang.NoSuchMethodException: calculateBMI [class android.view.View]
07-04 02:26:19.436: E/AndroidRuntime(26593): at java.lang.Class.getConstructorOrMethod(Class.java:472)
07-04 02:26:19.436: E/AndroidRuntime(26593): at java.lang.Class.getMethod(Class.java:857)
07-04 02:26:19.436: E/AndroidRuntime(26593): at android.view.View$1.onClick(View.java:3963)
07-04 02:26:19.436: E/AndroidRuntime(26593): ... 11 more
07-04 02:26:21.981: I/Process(26593): Sending signal. PID: 26593 SIG: 9
很抱歉没有发布logcat。将它与java代码一起附加。
答案 0 :(得分:1)
当您放置一个android:onClick on XML时,您需要一个在您的Activity上具有相同名称的方法来处理...不在您的View或Fragment上,而是在Acitivy中。
我还建议你阅读堆栈跟踪,这个解释也是如此。
答案 1 :(得分:0)
宣布观看&#39; rootView&#39;在全球。 然后通过rootView.findViewById
找到所有控件