片段中的按钮在android中不起作用
Posted
技术标签:
【中文标题】片段中的按钮在android中不起作用【英文标题】:Button in Fragment are not working in android 【发布时间】:2014-10-25 18:02:59 【问题描述】:我已经阅读了很多帖子和问题和答案,但我无法解决我的问题。它不工作。
我有一个带有来自 android Studio 的 Mainmenu-Activity 的应用程序。现在我有一些片段被设置到容器中(通过替换)。
现在我在 Fragment 中添加了一个按钮,并希望它能够进行一些计算。但这是问题所在。这个按钮没有做任何事情。
这是我的代码:
package eu.terratex.terratextoolbox;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.view.View.OnClickListener;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
/**
* A simple @link Fragment subclass.
* Activities that contain this fragment must implement the
* @link Knastrechner.OnFragmentInteractionListener interface
* to handle interaction events.
* Use the @link Knastrechner#newInstance factory method to
* create an instance of this fragment.
*
*/
public class Knastrechner extends Fragment
private OnFragmentInteractionListener mListener;
private Button btnClick;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment Knastrechner.
*/
// TODO: Rename and change types and number of parameters
public static Knastrechner newInstance()
Knastrechner fragment = new Knastrechner();
Bundle args = new Bundle();
return fragment;
public Knastrechner()
// Required empty public constructor
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
super.onCreateView(inflater, container, savedInstanceState);
// Inflate the layout for this fragment
View view = null;
view = inflater.inflate(R.layout.fragment_knastrechner, container, false);
Button mButton = (Button) view.findViewById(R.id.berechnenButton);
mButton.setOnClickListener(new OnClickListener()
@Override
public void onClick(View view)
System.out.println("abc");
);
return inflater.inflate(R.layout.fragment_knastrechner, container, false);
@Override
public void onAttach(Activity activity)
super.onAttach(activity);
try
mListener = (OnFragmentInteractionListener) activity;
catch (ClassCastException e)
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
@Override
public void onDetach()
super.onDetach();
mListener = null;
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
我在这里添加了onclick:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
super.onCreateView(inflater, container, savedInstanceState);
// Inflate the layout for this fragment
View view = null;
view = inflater.inflate(R.layout.fragment_knastrechner, container, false);
Button mButton = (Button) view.findViewById(R.id.berechnenButton);
mButton.setOnClickListener(new OnClickListener()
@Override
public void onClick(View view)
System.out.println("abc");
);
return inflater.inflate(R.layout.fragment_knastrechner, container, false);
谁能帮帮我?
【问题讨论】:
【参考方案1】:你应该使用return view;
而不是return inflater.inflate(R.layout.fragment_knastrechner, container, false);
,否则你只是放弃了你之前在代码中所做的事情。
【讨论】:
哦,谢谢,3 小时搜索答案的简单解决方案:P【参考方案2】:改成
return inflater.inflate(R.layout.fragment_knastrechner, container, false);
到
return view;
因为您已经在 view
对象中扩充了布局。
【讨论】:
【参考方案3】:如果没有写入返回视图,则返回视图后的片段 evrey 为 null,则编写的所有代码都将不起作用
enter code h super.onCreateView(inflater, container, savedInstanceState);
// Inflate the layout for this fragment
View view = null;
view = inflater.inflate(R.layout.fragment_knastrechner, container, false);
Button mButton = (Button) view.findViewById(R.id.berechnenButton);
mButton.setOnClickListener(new OnClickListener()
@Override
public void onClick(View view)
System.out.println("abc");
);
return view;
在
【讨论】:
以上是关于片段中的按钮在android中不起作用的主要内容,如果未能解决你的问题,请参考以下文章