在无线电组app崩溃时调用片段内的onCheckedCahngedListener时,我遇到了一些问题。 当点击广播组内的任何项目时,应用程序崩溃了。
fragment_sell.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.syedsaadali.myapplication.SellFragment"
android:orientation="vertical">
<HorizontalScrollView
android:id="@+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_gravity="center"
android:background="#FFFFFF"
android:fillViewport="true"
android:measureAllChildren="false"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="@+id/RadioButton1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:checkedButton="@+id/first"
>
<RadioButton
android:id="@+id/sApartment"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:button="@drawable/apartment"
android:padding="15dp"
android:onClick="apartment"/>
<TextView
android:layout_width="0.2dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="@mipmap/vertical" />
<RadioButton
android:id="@+id/sBuilder"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:button="@drawable/house"
android:padding="15dp"
android:onClick="house"/>
<TextView
android:layout_width="0.2dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="@mipmap/vertical" />
<RadioButton
android:id="@+id/sPlot"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:button="@drawable/land"
android:padding="15dp"
android:onClick="land"/>
<TextView
android:layout_width="0.2dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="@mipmap/vertical" />
<RadioButton
android:id="@+id/sHouse"
android:layout_width="100dp"
android:layout_height="match_parent"
android:button="@drawable/builder"
android:padding="15dp"
android:onClick="builder"/>
<TextView
android:layout_width="0.2dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="@mipmap/vertical" />
<RadioButton
android:id="@+id/sStudioApartment"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:button="@drawable/farmhouse"
android:padding="15dp"
android:onClick="farmhouse"/>
<TextView
android:layout_width="0.2dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="@mipmap/vertical" />
<RadioButton
android:id="@+id/sFarmHouse"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:button="@drawable/serviceapartment"
android:padding="15dp"
android:onClick="servicedapartment"/>
<TextView
android:layout_width="0.2dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="@mipmap/vertical" />
<RadioButton
android:id="@+id/sServicedApartment"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:button="@drawable/studioapartment"
android:padding="15dp"
android:onClick="studioapartment"/>
<TextView
android:layout_width="0.2dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:background="@mipmap/vertical" />
</RadioGroup>
</LinearLayout>
</HorizontalScrollView>
SellFragment.java
public class SellFragment extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
private RadioGroup mRadioGroup1;
private RadioButton mapartment;
private RadioButton mBuilder;
private RadioButton mplot;
private RadioButton mHouse;
private RadioButton mStudioApartment;
private RadioButton mFarmhouse;
private RadioButton mServicedApartment;
private Spinner city;
private View layout;
public static SellFragment newInstance(String param1, String param2) {
SellFragment fragment = new SellFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
layout = inflater.inflate(R.layout.fragment_sell, container, false);
mRadioGroup1=(RadioGroup) layout.findViewById(R.id.RadioButton1);
mapartment=(RadioButton)layout.findViewById(R.id.sApartment);
mBuilder=(RadioButton)layout.findViewById(R.id.sBuilder);
mplot=(RadioButton)layout.findViewById(R.id.sPlot);
mHouse=(RadioButton)layout.findViewById(R.id.sHouse);
mStudioApartment=(RadioButton)layout.findViewById(R.id.sStudioApartment);
mFarmhouse=(RadioButton)layout.findViewById(R.id.sFarmHouse);
mServicedApartment=(RadioButton)layout.findViewById(R.id.sServicedApartment);
mRadioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.sApartment) {
Toast.makeText(getActivity(), "Sell Fragment", Toast.LENGTH_SHORT).show();
} else if (checkedId == R.id.sBuilder) {
Toast.makeText(getActivity(), "Sell Fragment", Toast.LENGTH_SHORT).show();
}
else if (checkedId == R.id.sPlot) {
Toast.makeText(getActivity(), "Sell Fragment", Toast.LENGTH_SHORT).show();
}
else if (checkedId == R.id.sHouse) {
Toast.makeText(getActivity(), "Sell Fragment", Toast.LENGTH_SHORT).show();
}
else if (checkedId == R.id.sStudioApartment) {
Toast.makeText(getActivity(), "Sell Fragment", Toast.LENGTH_SHORT).show();
}
else if (checkedId == R.id.sFarmHouse) {
Toast.makeText(getActivity(), "Sell Fragment", Toast.LENGTH_SHORT).show();
}
else if (checkedId == R.id.cServicedApartment) {
Toast.makeText(getActivity(), "Sell Fragment", Toast.LENGTH_SHORT).show();
}
}
});
return layout;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}}
logcat的:
03-23 02:50:57.703 4697-4697/com.example.syedsaadali.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.syedsaadali.myapplication, PID: 4697
java.lang.IllegalStateException: Could not find method apartment(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatRadioButton with id 'cApartment'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:307)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:266)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:1)
您已在<RadioButton ... android:onClick="apartment"/>
中定义了XML
,但apartment
中未创建任何方法Activity
。
要解决此问题,请删除onClick
属性(如果需要注释)或在java中创建方法apartment
以及
public void apartment(View view){
}
您可以直接点击onClick
XML
属性中的 Alt + Enter ,以获得通过创建方法解决此错误的提示。 / p>