Android版的onClick方法不会被调用 [英] Android onClick method is not invoked

查看:154
本文介绍了Android版的onClick方法不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的onclick方法添加到我的 fragment2.xml

按钮

 <按钮
        机器人:ID =@ + ID / getTextButton
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=Kliknij做片段1
        安卓的onClick =的onClick/>

然后在Fragment2.java定义这个方法

 公共无效的onClick(视图查看){}

但是,当我点击它的应用程序崩溃?什么是完全错误
下面XML和Java文件

 包com.example.fragments;进口android.app.Fragment;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
公共类Fragment2扩展片段{
    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstance){
        Log.d(片段2,metoda);
        返回inflater.inflate(R.layout.fragment2,集装箱,FALSE);
    }    公共无效的onClick(视图查看){}}


 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:背景=#FFFF00>    <的TextView
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=片段2
        机器人:文字颜色=#0000FF
        机器人:TEXTSIZE =25sp/> &所述;! - moze BYC像素DP藻 - >    <按钮
        机器人:ID =@ + ID / getTextButton
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文字=Kliknij做片段1
        安卓的onClick =的onClick/>< / LinearLayout中>


解决方案

的onclick功能将与在布局充气的情况下被调用。

试试把onClick的功能在你的活动,而不是你的片段,它应该工作。

我有一个<一个href=\"http://stackoverflow.com/questions/6590988/onclick-attribute-in-xml-linking-to-method-in-activity-class\">similar之前的问题的。

I add the onclick method to my button in the fragment2.xml

   <Button
        android:id="@+id/getTextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Kliknij do fragment 1"
        android:onClick="onClick" />

Then defined this method in Fragment2.java

public void onClick(View view){}

But when I click on it the app crashes? What is wrong full XML and java file below

package com.example.fragments;

import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class Fragment2 extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
        Log.d("Fragment 2", "metoda");
        return inflater.inflate(R.layout.fragment2, container, false);
    }

    public void onClick(View view){}

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#FFFF00" >

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Fragment 2"
        android:textColor="#0000FF"
        android:textSize="25sp"/> <!-- moze byc px dp sp -->

    <Button
        android:id="@+id/getTextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Kliknij do fragment 1"
        android:onClick="onClick" />

</LinearLayout>

解决方案

The onClick function that will be called with be in the context of the layout inflater.

Try to put the onClick function in your Activity rather than your Fragment and it should work.

I had a similar problem before.

这篇关于Android版的onClick方法不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆