Android应用程序崩溃(片段和XML的onclick) [英] Android app crashing (fragment and xml onclick)
问题描述
second_fragment.xml
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / f2_layout
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的TextView
机器人:ID =@ + ID / f2_textview
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/ f2_tv/>
<按钮
机器人:ID =@ + ID / f2_button
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:的onClick =按钮
机器人:文本=@字符串/ f2_bttn/>
SecondFragment.java
公共类SecondFragment扩展片段{
FragmentInterface iface的;
公共接口FragmentInterface {
公共无效按钮pressed();
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
返回inflater.inflate(R.layout.second_fragment,集装箱,假);
}
@覆盖
公共无效onAttach(活动活动){
super.onAttach(活动);
//这将确保集装箱活动已实施
//回调接口。如果不是,它抛出一个异常
尝试 {
IFACE =(FragmentInterface)的活动;
}赶上(ClassCastException异常E){
抛出新ClassCastException异常(activity.toString()
+必须实现FragmentInterface);
}
}
公共无效按钮(查看视图){
}
}
我是新手,我不知道为什么我的应用程序崩溃,当pressed按钮?谁能解释?
01-03 13:28:25.612:E / AndroidRuntime(1276):致命异常:主要
01-03 13:28:25.612:E / AndroidRuntime(1276):java.lang.IllegalStateException:找不到在活动课com.sp.fragments.MainActivity对视图类的android onclick处理方法按钮(查看)。 widget.Button ID为'f2_button
01-03 13:28:25.612:E / AndroidRuntime(1276年):在android.view.View $ 1.onClick(View.java:3584)
01-03 13:28:25.612:E / AndroidRuntime(1276年):在android.view.View.performClick(View.java:4202)
01-03 13:28:25.612:E / AndroidRuntime(1276年):在android.view.View $ PerformClick.run(View.java:17340)
01-03 13:28:25.612:E / AndroidRuntime(1276年):在android.os.Handler.handleCallback(Handler.java:725)
01-03 13:28:25.612:E / AndroidRuntime(1276年):在android.os.Handler.dispatchMessage(Handler.java:92)
01-03 13:28:25.612:E / AndroidRuntime(1276年):在android.os.Looper.loop(Looper.java:137)
01-03 13:28:25.612:E / AndroidRuntime(1276年):在android.app.ActivityThread.main(ActivityThread.java:5039)
01-03 13:28:25.612:E / AndroidRuntime(1276年):在java.lang.reflect.Method.invokeNative(本机方法)
01-03 13:28:25.612:E / AndroidRuntime(1276年):在java.lang.reflect.Method.invoke(Method.java:511)
01-03 13:28:25.612:E / AndroidRuntime(1276年):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
01-03 13:28:25.612:E / AndroidRuntime(1276年):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-03 13:28:25.612:E / AndroidRuntime(1276年):在dalvik.system.NativeStart.main(本机方法)
01-03 13:28:25.612:E / AndroidRuntime(1276):java.lang.NoSuchMethodException:产生的原因按钮[类android.view.View]
01-03 13:28:25.612:E / AndroidRuntime(1276年):在java.lang.Class.getConstructorOrMethod(Class.java:460)
01-03 13:28:25.612:E / AndroidRuntime(1276年):在java.lang.Class.getMethod(Class.java:915)
01-03 13:28:25.612:E / AndroidRuntime(1276年):在android.view.View $ 1.onClick(View.java:3577)
01-03 13:28:25.612:E / AndroidRuntime(1276):11 ...更多
01-03 13:28:27.563:I /过程(1276年):发送信号。 PID:1276 SIG:9
活动:
如果是具有活性的,如果你定义了机器人:的onClick
的XML属性,那么你只需要在活动相同的名称定义一个方法
片段:
不过,只要你有片段,如果你想通过只定义来定义点击监听器安卓的onClick
属性,那么你必须定义与实际活动相同名称的方法从那里片段已被调用
或否则,你可以简单地通过编程实现点击监听器。
second_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/f2_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/f2_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/f2_tv" />
<Button
android:id="@+id/f2_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="button"
android:text="@string/f2_bttn" />
SecondFragment.java
public class SecondFragment extends Fragment {
FragmentInterface iface;
public interface FragmentInterface {
public void buttonPressed();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.second_fragment, container, false);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
iface = (FragmentInterface) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement FragmentInterface");
}
}
public void button(View view) {
}
}
I'm newbie and I have no idea why my application crash, when button is pressed ? Can anyone explain?
01-03 13:28:25.612: E/AndroidRuntime(1276): FATAL EXCEPTION: main
01-03 13:28:25.612: E/AndroidRuntime(1276): java.lang.IllegalStateException: Could not find a method button(View) in the activity class com.sp.fragments.MainActivity for onClick handler on view class android.widget.Button with id 'f2_button'
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.view.View$1.onClick(View.java:3584)
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.view.View.performClick(View.java:4202)
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.view.View$PerformClick.run(View.java:17340)
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.os.Handler.handleCallback(Handler.java:725)
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.os.Handler.dispatchMessage(Handler.java:92)
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.os.Looper.loop(Looper.java:137)
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-03 13:28:25.612: E/AndroidRuntime(1276): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 13:28:25.612: E/AndroidRuntime(1276): at java.lang.reflect.Method.invoke(Method.java:511)
01-03 13:28:25.612: E/AndroidRuntime(1276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-03 13:28:25.612: E/AndroidRuntime(1276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-03 13:28:25.612: E/AndroidRuntime(1276): at dalvik.system.NativeStart.main(Native Method)
01-03 13:28:25.612: E/AndroidRuntime(1276): Caused by: java.lang.NoSuchMethodException: button [class android.view.View]
01-03 13:28:25.612: E/AndroidRuntime(1276): at java.lang.Class.getConstructorOrMethod(Class.java:460)
01-03 13:28:25.612: E/AndroidRuntime(1276): at java.lang.Class.getMethod(Class.java:915)
01-03 13:28:25.612: E/AndroidRuntime(1276): at android.view.View$1.onClick(View.java:3577)
01-03 13:28:25.612: E/AndroidRuntime(1276): ... 11 more
01-03 13:28:27.563: I/Process(1276): Sending signal. PID: 1276 SIG: 9
Activity:
If are having activity and if you define android:onClick
attribute in XML then you just need to define a method with the same name in Activity.
Fragment:
But whenever you have Fragment and if you want to define click listener by just defining android:onClick
attribute then you have to define a method with the same name in actual activity from where Fragment has been called.
OR and otherwise, you can simply implement a click listener programmatically.
这篇关于Android应用程序崩溃(片段和XML的onclick)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!