我有两个被片段覆盖的类。
startActivity()方法返回错误。
当单击我的home.java类中名为btn的按钮时。我想在名为hometwo.java的类中单击名为btnctwo的按钮。
但是我得到一个错误。
logcat
.sahnartsappone.sahnartsapp.hometwo}; have you declared this activity in your AndroidManifest.xml?
我是否需要将hometwo.java类添加到我的AndroidManifest文件中,如下面的示例代码所示?
<activity
android:name=".hometwo"
android:screenOrientation="portrait" />
我已将我的hometwo.java类添加到我的AndroidManifest文件中,如上述代码示例中所示。但是我出错了。
为什么我需要将hometwo.java类添加到我的AndroidManifest文件中?
home.java
public class home extends Fragment {
Button btn;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home, container, false);
tn = rootView.findViewById(R.id.btnc);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), hometwo.class);
intent.putExtra("Indicator", true);
startActivity(intent);
}
});
return rootView;
}
}
hometwo.java
public class hometwo extends Fragment {
Button btnctwo;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home, container, false);
btnctwo = rootView.findViewById(R.id.btnctwo);
Bundle b = getActivity().getIntent().getExtras();
if (b != null) {
if (b.containsKey("Indicator"))
btnctwo.performClick();
}
btnctwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("testler","testlertwo");
}
});
return rootView;
}
}
logcat
2020-05-17 16:21:10.944 9826-9826/com.sahnartsappone.sahnartsapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sahnartsappone.sahnartsapp, PID: 9826
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.sahnartsappone.sahnartsapp/com.sahnartsappone.sahnartsapp.hometwo}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2049)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1709)
at android.app.Activity.startActivityForResult(Activity.java:5192)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:234)
at androidx.fragment.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:791)
at androidx.fragment.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:933)
at androidx.fragment.app.Fragment.startActivity(Fragment.java:1185)
at androidx.fragment.app.Fragment.startActivity(Fragment.java:1173)
at com.sahnartsappone.sahnartsapp.home$1.onClick(home.java:29)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
此问题没有答案。
这不是您开始片段的方式。你正在尝试开始一个片段作为一项活动,它不是@a_local_nobody
startActicity()方法在由片段扩展的类中不起作用。
Intent intent = new Intent(getActivity(), hometwo.class);
intent.putExtra("Indicator", true);
startActivity(intent);
感谢@a_local_nobody