我设法制作了一个导航抽屉并试图让按钮立即工作。
代码:
public class FourthFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fourth_layout, container, false);
}
public void buttona1(View view){
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.google.nl"));
startActivity(browserIntent);
}
}
布局:
<Button
android:text="Magister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="buttona1"
android:id="@+id/button2"
android:visibility="visible"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
问题在于,当我在手机中打开应用程序并转到按钮页面时,按下按钮,应用程序将关闭并停止工作。
答案 0 :(得分:0)
您应初始化Context
中的fragment contsructor
,MainActivity
会在初始化freagment
时通过context.startActivity(YOUR_INTENT);
传递,并致电DECLARE
q_id_from_update varchar2(45);
q_id_to_update varchar2(45);
BEGIN
SELECT QUESTION_ID
INTO q_id_to_update
FROM QUESTION
WHERE QUESTION.NAME = 'QUESTION_A';
SELECT QUESTION_ID
INTO q_id_from_update
FROM QUESTION
WHERE QUESTION.NAME = 'QUESTION_B';
FOR a IN (SELECT
ANSWER.QUESTION_ID,
ANSWER.USER_ID,
ANSWER.ANSWER
FROM ANSWER
WHERE ANSWER.QUESTION_ID = q_id_from_update AND
ANSWER.ANSWER IS NOT NULL)
LOOP
UPDATE ANSWER
SET ANSWER.ANSWER = a.ANSWER
WHERE ANSWER.USER_ID = a.USER_ID AND
ANSWER.QUESTION_ID = q_id_to_update;
END LOOP;
COMMIT;
END;
/
希望这会对你有所帮助
答案 1 :(得分:0)
您的代码似乎正确,但您需要在活动中创建方法buttona1
和片段两者。
你不能从xml中调用该方法驻留在Fragment中,而不是在Activity
您可以在onCreateView
方法
View v=inflater.inflate(R.layout.fourth_layout, container, false);
Button myButton=(Button)v.findViewById(R.id.button2);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
buttona1(view)
});
return v;
也替换此行
startActivity(browserIntent);
带
getActivity().startActivity(browserIntent);