我创建了一个包含3页的viewpager。目前其中一个有一个按钮,但是当我尝试向按钮添加一个按钮监听器时,应用程序崩溃但调试没有报告任何错误。除了断开连接到虚拟机。 最初视图被夸大了,听众在案例陈述中添加了内容,但这也没有用。
如果Android有问题,控制台不应该说些什么吗?
片段及其行为如下 应用程序在setOnClickListener上崩溃。 有任何想法吗? 谢谢
<?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:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity$PlaceholderFragment">
<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/app_name"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/constraintLayout"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1" />
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintVertical_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
View mainView = inflater.inflate(R.layout.fragment_main, container, false);
button= (Button) getActivity().findViewById(R.id.imageButton2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
TextView textView = (TextView)getActivity().findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
}
});
int page=getArguments().getInt(ARG_SECTION_NUMBER);
switch (page)
{
case 1:
rootView=mainView;
break;
case 2:
rootView = inflater.inflate(R.layout.activity_options, container, false);
break;
case 3:
rootView = inflater.inflate(R.layout.activity_request, container, false);
break;
default:
rootView = inflater.inflate(R.layout.fragment_main, container, false);
break;
}
return rootView;
}
答案 0 :(得分:1)
findViewById
试试这个
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView;
View mainView= inflater.inflate(R.layout.fragment_main, container, false);
ImageButton button= (ImageButton) mainView.findViewById(R.id.imageButton2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
TextView textView = (TextView)mainView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
}
});
int page=getArguments().getInt(ARG_SECTION_NUMBER);
switch (page)
{
case 1:
rootView=mainView;
break;
case 2:
rootView = inflater.inflate(R.layout.activity_options, container, false);
break;
case 3:
rootView = inflater.inflate(R.layout.activity_request, container, false);
break;
default:
rootView = inflater.inflate(R.layout.fragment_main, container, false);
break;
}
return rootView;
}
答案 1 :(得分:1)
可能会出现多个出错的情况,其中一个我认为ImageButton无法转换为Button。如果不是这种情况,请尝试使用调试器来查看null(最可能是null)
答案 2 :(得分:1)
更改以下内容
button= (Button) getActivity().findViewById(R.id.imageButton2);
到
button= (Button) mainView.findViewById(R.id.imageButton2);
并在充气视图后初始化button
。因为它可能不会在初始化时产生问题,但如果您的视图更改了实际按钮的位置,则会崩溃。
答案 3 :(得分:0)
由于上面的答案解决方案是改变一行 从 button =(Button)getActivity()。findViewById(R.id.imageButton2); 到
ImageButton button = mainView.findViewById(R.id.imageButton2);