我已经单独测试了功能(没有片段),并且效果很好。但是,当我在主项目(使用片段)中实现该功能时,当我单击buttonSearch时,应用程序强制关闭。如有必要,我可以包含SearchActivity.class代码。
HomeFragment代码:
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
public class HomeFragment extends Fragment {
Button buttonSearch;
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
buttonSearch = view.findViewById(R.id.buttonSearch);
buttonSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), SearchActivity.class);
startActivity(intent);
}
});
return view;
}
}
fragment_home代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/buttonSearch"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:background="@drawable/search_outline"
android:paddingLeft="10dp"
android:shadowColor="#B8B6B6"
android:text="Search here"
android:textAlignment="viewStart"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#999999"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
AndroidManifest代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kitchen">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".SchoolProfile"></activity>
<activity android:name=".AccountActivity" />
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat(红线用“ **”括起来):
**2019-12-26 12:23:49.409 30939-30939/? E/Zygote: isWhitelistProcess - Process is Whitelisted
2019-12-26 12:23:49.410 30939-30939/? E/Zygote: accessInfo : 1**
2019-12-26 12:23:49.413 30939-30939/? I/SELinux: SELinux: seapp_context_lookup: seinfo=default, level=s0:c8,c257,c512,c768, pkgname=com.example.kitchen
2019-12-26 12:23:49.416 30939-30939/? I/example.kitche: Late-enabling -Xcheck:jni
2019-12-26 12:23:49.543 30939-30939/com.example.kitchen D/ConnectivityManager_URSP: Ursp sIsUrsp=false, sIsCheckUrsp=false, uid=10264
2019-12-26 12:23:49.545 30939-30939/com.example.kitchen D/Proxy: urspP is null: 10264
2019-12-26 12:23:49.744 30939-30939/com.example.kitchen W/ComponentDiscovery: Application info not found.
2019-12-26 12:23:49.744 30939-30939/com.example.kitchen W/ComponentDiscovery: Could not retrieve metadata, returning empty list of registrars.
2019-12-26 12:23:49.751 30939-30939/com.example.kitchen D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
2019-12-26 12:23:49.751 30939-30939/com.example.kitchen D/FirebaseApp: com.google.firebase.iid.FirebaseInstanceId is not linked. Skipping initialization.
2019-12-26 12:23:49.752 30939-30939/com.example.kitchen D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
2019-12-26 12:23:49.752 30939-30939/com.example.kitchen D/FirebaseApp: com.google.android.gms.measurement.AppMeasurement is not linked. Skipping initialization.
2019-12-26 12:23:49.752 30939-30939/com.example.kitchen I/FirebaseInitProvider: FirebaseApp initialization successful
2019-12-26 12:23:49.786 30939-30970/com.example.kitchen D/libEGL: loaded /vendor/lib64/egl/libGLES_mali.so
2019-12-26 12:23:49.813 30939-30939/com.example.kitchen W/example.kitche: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
2019-12-26 12:23:49.813 30939-30939/com.example.kitchen W/example.kitche: Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking)
2019-12-26 12:23:49.813 30939-30939/com.example.kitchen W/example.kitche: Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking)
2019-12-26 12:23:49.813 30939-30939/com.example.kitchen W/example.kitche: Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking)
2019-12-26 12:23:49.813 30939-30939/com.example.kitchen W/example.kitche: Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking)
2019-12-26 12:23:49.861 30939-30939/com.example.kitchen W/example.kitche: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2019-12-26 12:23:49.862 30939-30939/com.example.kitchen W/example.kitche: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2019-12-26 12:23:49.922 30939-30939/com.example.kitchen W/example.kitche: Accessing hidden method Landroid/widget/TextView;->getTextDirectionHeuristic()Landroid/text/TextDirectionHeuristic; (light greylist, linking)
2019-12-26 12:23:50.026 30939-30939/com.example.kitchen D/OpenGLRenderer: Skia GL Pipeline
2019-12-26 12:23:50.028 30939-30939/com.example.kitchen D/EmergencyMode: [EmergencyManager] android createPackageContext successful
2019-12-26 12:23:50.043 30939-30939/com.example.kitchen D/InputTransport: Input channel constructed: fd=61
2019-12-26 12:23:50.043 30939-30939/com.example.kitchen D/ViewRootImpl@72ef8f5[MainActivity]: setView = DecorView@158d68a[MainActivity] TM=true MM=false
2019-12-26 12:23:50.061 30939-30939/com.example.kitchen D/ViewRootImpl@72ef8f5[MainActivity]: dispatchAttachedToWindow
2019-12-26 12:23:50.093 30939-30939/com.example.kitchen D/ViewRootImpl@72ef8f5[MainActivity]: Relayout returned: old=[0,0][1440,2960] new=[0,0][1440,2960] result=0x7 surface={valid=true 507980353536} changed=true
2019-12-26 12:23:50.106 30939-30971/com.example.kitchen I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2019-12-26 12:23:50.106 30939-30971/com.example.kitchen I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2019-12-26 12:23:50.106 30939-30971/com.example.kitchen I/OpenGLRenderer: Initialized EGL, version 1.4
2019-12-26 12:23:50.106 30939-30971/com.example.kitchen D/OpenGLRenderer: Swap behavior 2
2019-12-26 12:23:50.116 30939-30971/com.example.kitchen D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
2019-12-26 12:23:50.116 30939-30971/com.example.kitchen D/OpenGLRenderer: eglCreateWindowSurface = 0x763d868e80, 0x7645fd1010
2019-12-26 12:23:50.299 30939-30939/com.example.kitchen D/ViewRootImpl@72ef8f5[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1 1
2019-12-26 12:23:50.308 30939-30939/com.example.kitchen D/InputMethodManager: prepareNavigationBarInfo() DecorView@158d68a[MainActivity]
2019-12-26 12:23:50.309 30939-30939/com.example.kitchen D/InputMethodManager: getNavigationBarColor() -855310
2019-12-26 12:23:50.314 30939-30939/com.example.kitchen D/InputMethodManager: prepareNavigationBarInfo() DecorView@158d68a[MainActivity]
2019-12-26 12:23:50.314 30939-30939/com.example.kitchen D/InputMethodManager: getNavigationBarColor() -855310
2019-12-26 12:23:50.314 30939-30939/com.example.kitchen V/InputMethodManager: Starting input: tba=com.example.kitchen ic=null mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
2019-12-26 12:23:50.315 30939-30939/com.example.kitchen D/InputMethodManager: startInputInner - Id : 0
2019-12-26 12:23:50.315 30939-30939/com.example.kitchen I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
2019-12-26 12:23:50.322 30939-30939/com.example.kitchen D/ViewRootImpl@72ef8f5[MainActivity]: MSG_RESIZED: frame=Rect(0, 0 - 1440, 2960) ci=Rect(0, 84 - 0, 0) vi=Rect(0, 84 - 0, 0) or=1
2019-12-26 12:23:50.331 30939-30955/com.example.kitchen D/InputTransport: Input channel constructed: fd=72
2019-12-26 12:23:50.332 30939-30939/com.example.kitchen D/InputMethodManager: prepareNavigationBarInfo() DecorView@158d68a[MainActivity]
2019-12-26 12:23:50.332 30939-30939/com.example.kitchen D/InputMethodManager: getNavigationBarColor() -855310
2019-12-26 12:23:50.332 30939-30939/com.example.kitchen V/InputMethodManager: Starting input: tba=com.example.kitchen ic=null mNaviBarColor -855310 mIsGetNaviBarColorSuccess true , NavVisible : true , NavTrans : false
2019-12-26 12:23:50.332 30939-30939/com.example.kitchen D/InputMethodManager: startInputInner - Id : 0
2019-12-26 12:23:56.303 30939-30939/com.example.kitchen D/ViewRootImpl@72ef8f5[MainActivity]: ViewPostIme pointer 0
2019-12-26 12:23:56.364 30939-30939/com.example.kitchen D/ViewRootImpl@72ef8f5[MainActivity]: ViewPostIme pointer 1
2019-12-26 12:23:56.378 30939-30939/com.example.kitchen D/AndroidRuntime: Shutting down VM
**2019-12-26 12:23:56.383 30939-30939/com.example.kitchen E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kitchen, PID: 30939
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.kitchen/com.example.kitchen.SearchActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2016)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673)
at android.app.Activity.startActivityForResult(Activity.java:4688)
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.example.kitchen.HomeFragment$1.onClick(HomeFragment.java:23)
at android.view.View.performClick(View.java:7333)
at android.widget.TextView.performClick(TextView.java:14160)
at android.view.View.performClickInternal(View.java:7299)
at android.view.View.access$3200(View.java:846)
at android.view.View$PerformClick.run(View.java:27774)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:6981)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)**
2019-12-26 12:23:56.399 30939-30939/com.example.kitchen I/Process: Sending signal. PID: 30939 SIG: 9