[英]How to move from a fragment of a navigation drawer to an Activity on Android Studio?
So, this is my first question here.所以,这是我在这里的第一个问题。 I am doing a project on Android Studio and I cannot seem to move from a fragment of a navigation drawer activity to one another.
我正在 Android Studio 上做一个项目,我似乎无法从导航抽屉活动的片段移动到另一个片段。 I've tried the solutions provided here earlier such as using:-
我已经尝试了前面提供的解决方案,例如使用:-
Intent i = new Intent(getActivity(), Main3Activity.class);
Intent i = new Intent(getActivity(), Main3Activity.class);
startActivity(i);
开始活动(i);
The intent keeps taking me to MainActivity instead of Main3Activity.意图一直将我带到 MainActivity 而不是 Main3Activity。 Here's my DetectFragment.java code:-
这是我的 DetectFragment.java 代码:-
public class DetectFragment extends Fragment {
private DetectViewModel detectViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
detectViewModel =
ViewModelProviders.of(this).get(DetectViewModel.class);
View root = inflater.inflate(R.layout.fragment_detect, container, false);
Button btnOpen = root.findViewById(R.id.btn1);
btnOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v){
Intent in = new Intent(getActivity(), Main3Activity.class);
startActivity(in);
}
});
return root;
}
}
If anyone has any solution, please do reply.如果有人有任何解决方案,请回复。 Thanks.
谢谢。
Your code should start Main3Activity.class
when pressing btnOpen
.您的代码应该开始
Main3Activity.class
当按下btnOpen
。
I think your problem is that Main3Activity.class
is crashing and being instantly destroyed.我认为您的问题是
Main3Activity.class
正在崩溃并立即被销毁。
See LogCat
见
LogCat
I found the problem.我发现了问题。 I had copied the code in Main3Activity and hadn't paid attention to the setContentView() layout and it was set to MainActivity.
之前复制了Main3Activity中的代码,没注意setContentView()布局,设置为MainActivity。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.