获取片段布局返回null

zsbz8rwp  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(217)

一段时间后回到安卓系统,研究gps定位的权限。这似乎很难组合在一起,我很失望我找不到这样一个普通事情的单一参考。
我有我的片段homefragment,来自android studio starter和选项卡式应用程序。在顶部,我有:

private View mainLayout;

在我的oncreateview()中:

binding = FragmentHomeBinding.inflate(inflater, container, false);
    View root = binding.getRoot();
    mainLayout = root.findViewById(R.id.main_layout_home);

然后,我的getlocation():

private Location getLocation() {
    Context context = getContext();
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    Location location;
    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED
        && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    } else {
        requestLocationPermssions();
        location = null;
    }
    return location;
}

private void requestLocationPermssions() {
    Activity activity = MainActivity.mainActivity;
    // Permission has not been granted and must be requested.
    if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
            Manifest.permission.ACCESS_FINE_LOCATION)) {
        // Provide an additional rationale to the user if the permission was not granted
        // and the user would benefit from additional context for the use of the permission.
        // Display a SnackBar with cda button to request the missing permission.
        Snackbar.make(mainLayout, R.string.location_access_required,
                Snackbar.LENGTH_INDEFINITE).setAction(R.string.ok, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Request the permission
                ActivityCompat.requestPermissions(activity,
                        new String[]{Manifest.permission.CAMERA},
                        ACCESS_FINE_LOCATION);
            }
        }).show();

    } else {
        Snackbar.make(mainLayout, R.string.location_access_required, Snackbar.LENGTH_SHORT).show();  // Blows up here
        // Request the permission. The result will be received in onRequestPermissionResult().
        ActivityCompat.requestPermissions(activity,
                new String[]{Manifest.permission.CAMERA}, ACCESS_FINE_LOCATION);
    }
}

下面的错误指向标记的行:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.view.View.getResources()' on a null object reference
    at com.google.android.material.snackbar.Snackbar.make(Snackbar.java:218)
    at us.square1tech.wb5000dispenser.ui.home.HomeFragment.requestLocationPermssions(HomeFragment.java:103)
    at us.square1tech.wb5000dispenser.ui.home.HomeFragment.getLocation(HomeFragment.java:77)
    at us.square1tech.wb5000dispenser.ui.home.HomeFragment.onCreateView(HomeFragment.java:42)
    at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
    at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2629)
    at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
    at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2722)
    at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:346)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1188)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
    at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625)
    at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
    at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:247)
    at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:541)
    at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:210)
    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
    at android.app.Activity.performStart(Activity.java:7157)
    at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2968)
    at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
    at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1836)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6710)
    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:911)

因为 mainLayout 是空的。
那么,如何获得片段的布局呢?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题