试图从片段开始活动,但是它不起作用,我也找不到问题 而且社区中没有其他可行的答案 当单击tn(button7)时,没有任何反应 在2个模拟器和华为电话中试用过
** sst.java(片段)**
public class sst extends Fragment {
Button btn ;
@Override
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(getContext(),"اختر مادة الكتاب",Toast.LENGTH_SHORT).show();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_sst, container, false);
btn = (Button) v.findViewById(R.id.button7);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), BooksExploreActivity.class) ;
intent.putExtra("subj" , "3olomTabe3ya") ;
getActivity().startActivity(intent);
}
});
return inflater.inflate(R.layout.fragment_sst, container, false);
}
}
fragment_sst.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:fontFamily="@font/monadi"
android:text="اختر مادة الكتاب"
android:textColor="@color/colorPrimary"
android:textSize="24dp" />
<Button
android:id="@+id/button7"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button13"
android:layout_alignStart="@+id/button13"
android:layout_below="@+id/textView19"
android:layout_marginTop="8dp"
android:background="@drawable/fragmentsbutton"
android:text="العلوم الطبيعية"
android:textColor="#fff"
android:textSize="19dp"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button7"
android:layout_alignStart="@+id/button7"
android:layout_below="@+id/button7"
android:layout_marginTop="19dp"
android:background="@drawable/fragmentsbutton"
android:text="الرياضيات"
android:textColor="#fff"
android:textSize="18dp"
android:id="@+id/button8" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
谢谢。
答案 0 :(得分:0)
更改
return inflater.inflate(R.layout.fragment_sst, container, false);
使用
return v;
使用您的解决方案,您将返回原始视图,而按钮上没有“自定义操作”。
答案 1 :(得分:0)
查看问题是您使用View v进行了逻辑处理,但是随后又返回了一个逻辑,则必须对此进行更改:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_sst, container, false);
btn = (Button) v.findViewById(R.id.button7);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), BooksExploreActivity.class) ;
intent.putExtra("subj" , "3olomTabe3ya") ;
getActivity().startActivity(intent);
}
});
return inflater.inflate(R.layout.fragment_sst, container, false); // change this
}
收件人:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_sst, container, false);
btn = (Button) v.findViewById(R.id.button7);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), BooksExploreActivity.class) ;
intent.putExtra("subj" , "3olomTabe3ya") ;
getActivity().startActivity(intent);
}
});
return v; // here is where the change is
}