这里的错误:
java.lang.IllegalStateException: Could not find method OpenRadio(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageButton with id 'RadioPootis'
Java代码:
/**
* A simple {@link Fragment} subclass.
* Use the {@link Fragment1#newInstance} factory method to
* create an instance of this fragment.
*/
public class Fragment1 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;
public Fragment1() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Fragment1.
*/
// TODO: Rename and change types and number of parameters
public static Fragment1 newInstance(String param1, String param2) {
Fragment1 fragment = new Fragment1();
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
return inflater.inflate(R.layout.fragment_1, container, false);
}
String url = "https://epicmario71.tk";
public void OpenRadio(View view) {
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(getActivity(), Uri.parse(url));
}
}
xml文件:
?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/background"
tools:context=".ui.main.Fragment1">
<ImageView
android:id="@+id/imageView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="18dp"
android:layout_marginLeft="18dp"
android:layout_marginTop="15dp"
android:src="@drawable/webp_net_resizeimage__6_"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginLeft="22dp"
android:layout_marginTop="15dp"
android:src="@drawable/webp_net_resizeimage__6_"
app:layout_constraintStart_toEndOf="@+id/RadioPootis"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginLeft="22dp"
android:layout_marginTop="15dp"
android:src="@drawable/webp_net_resizeimage__6_"
app:layout_constraintStart_toEndOf="@+id/imageView12"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/RadioPootis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="22dp"
android:layout_marginLeft="22dp"
android:layout_marginTop="16dp"
android:background="@null"
android:onClick="OpenRadio"
android:src="@drawable/webp_net_resizeimage__6_"
app:layout_constraintStart_toEndOf="@+id/imageView11"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
当我单击按钮时,应用程序崩溃
使用Android Studio中的选项卡式活动示例旧版Android Studio库
欢迎任何帮助
android:onClick
除非在活动类而非片段类中创建属性,否则不能在片段布局中使用属性。
在您的情况下:
Here the error: java.lang.IllegalStateException: Could not find method OpenRadio(View) in a parent or ancestor Context for android:onClick attribute defined on view class
系统查看托管的活动类,Fragment1
但未找到OpenRadio(View)
方法。
因此,如果OpenRadio(View)
方法所持有的代码与片段无关,则需要将方法移至活动类。或者在OpenRadio(View)
调用Activity时需要在片段和Activity之间建立一些通信,对于这一部分,您可以在此处检查答案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句