【问题标题】:Buttons don't work in android fragments按钮在 android 片段中不起作用
【发布时间】:2014-02-15 07:15:26
【问题描述】:

我正在开发一个应用程序,我有以下代码:

package com.S.A.Productions.android.first;

import com.S.A.Productions.android.first.R;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class FirstActivity extends Fragment implements OnClickListener {

    int counter;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

                View v = inflater.inflate(R.layout.lin, container, false);

        TextView temp = (TextView) v.findViewById(R.id.textView2);

        //Set the buttons
        Button button2 = (Button) v.findViewById(R.id.button2);


        //+++ BUTTON
        button2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub//Get content of TextView
                TextView temp = (TextView) v.findViewById(R.id.textView2);
                //Convert the string to an integer
                counter = Integer.parseInt(temp.getText().toString());
                counter++;
                temp.setText("" + counter);
                String stringData = temp.getText().toString();
                SharedPreferences.Editor editor = someData.edit();
                editor.putString("sharedString", stringData);
                editor.commit();
            }

        });
        //END OF +++ BUTTON

        return v;
    }


}

但是当我运行应用程序并单击该按钮时,应用程序崩溃了。 我正在使用“v.findViewById” 最后我返回 v。所以我不知道到底出了什么问题。 有什么想法吗?

【问题讨论】:

  • 您可以发布跟踪和您的布局 lin xml 吗?

标签: java android button onclick android-inflate


【解决方案1】:

冲突是因为onclick里面的View v和inflated view v不一样。此代码将起作用:

public class FirstActivity extends Fragment implements OnClickListener {

int counter;
View v ;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

            v = inflater.inflate(R.layout.lin, container, false);

    TextView temp = (TextView) v.findViewById(R.id.textView2);

    //Set the buttons
    Button button2 = (Button) v.findViewById(R.id.button2);


    //+++ BUTTON
    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub//Get content of TextView
            TextView temp = (TextView) this.v.findViewById(R.id.textView2);
            //Convert the string to an integer
            counter = Integer.parseInt(temp.getText().toString());
            counter++;
            temp.setText("" + counter);
            String stringData = temp.getText().toString();
            SharedPreferences.Editor editor = someData.edit();
            editor.putString("sharedString", stringData);
            editor.commit();
        }

    });
    //END OF +++ BUTTON

    return v;
  }
}

【讨论】:

  • 我明白了。我刚刚将“v”更改为“myView”,现在一切正常!非常感谢,我已经尝试了好几个小时了。
  • 我没有足够的代表 :(
  • 您可以在没有代表的情况下“投票”和“勾选”问题中的答案。
【解决方案2】:

onClick(View v) 的参数 v 是被点击的元素,在你的例子中是 button

错误是您正在使用参数 V(这是一个 button)作为视图组,试图通过 Id 从 button 本身查找您的文本视图。 textView 将为空。 setText() 会导致崩溃。

您需要在视图层次结构中textView 的任何容器上使用findviewbyid()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多