简体   繁体   English

添加 displayNotification() 后,Android 应用程序不断崩溃

[英]Android app keeps crashing after displayNotification() added

so the problem is following - My react-native android app keeps crashing, when I try to open it.所以问题出现了 - 当我尝试打开它时,我的 react-native android 应用程序不断崩溃。 This started after adding CreateNotificationChannel() and SendNotification() functions.这是在添加 CreateNotificationChannel() 和 SendNotification() 函数后开始的。 Would like to get some help or tips about my problem.想就我的问题获得一些帮助或提示。 This is my code.这是我的代码。 As I figured out, there is something wrong about the intent.正如我发现的那样,意图有问题。

package com.notifikacijas;
import android.util.Log;
import android.widget.Toast;

import android.content.Context;
import android.content.Intent;
import android.app.NotificationManager;
import android.app.NotificationChannel;
import android.app.Notification;
import android.app.PendingIntent;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.NotificationCompat.Builder;
import com.facebook.react.bridge.ReactApplicationContext;
import android.net.Uri;
public class NotificationService extends ReactContextBaseJavaModule {
  private ReactApplicationContext reactContext;

  public NotificationService(ReactApplicationContext reactContext) {
    super(reactContext);
    Log.i("NotificationService", "NotificationService Constructor");
    this.reactContext = reactContext;
    String CHANNEL_ID = "Kanaals";
    NotificationCompat.Builder builders = new NotificationCompat.Builder(this.reactContext, CHANNEL_ID);
    builders.setContentTitle("Virsraksts");
    builders.setContentText("Teksts");
    // createNotificationChannel();
createNotificationChannel();
displayNotification();

  }

  private void displayNotification() {
  Intent intent = this.reactContext.getPackageManager().getLaunchIntentForPackage("dtg");

  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

  Uri uri = intent.getData();

  Log.i("NotificationService", "URI "+uri.toString());
  PendingIntent pendingIntent = PendingIntent.getActivity(this.reactContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

  String CHANNEL_ID = "notificationsChannel";
  NotificationCompat.Builder builder = new NotificationCompat.Builder(this.reactContext, CHANNEL_ID);
  // builder.setSmallIcon(17301575);
  builder.setContentTitle("Objektā atrodas darbinieki");
  builder.setContentText("Vai vēlaties izreģistrēt darbiniekus no objekta?");
  builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
  builder.setContentIntent(pendingIntent);
  // builder.setAutoCancel(false);

  NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this.reactContext);
  notificationManager.notify(123, builder.build());
  }

  private void createNotificationChannel() {
String CHANNEL_ID = "notificationsChannel";
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = new StringBuffer("charsequence");
String description = "normals description";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = this.reactContext.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}

  @Override
  public String getName() {
    return "NotificationService";
  }
}

react-native: 0.61.5反应原生:0.61.5

EDIT:: Error log (logcat)编辑::错误日志(logcat)

03-13 10:16:45.420 26359 26359 E AndroidRuntime: FATAL EXCEPTION: main
03-13 10:16:45.420 26359 26359 E AndroidRuntime: Process: com.notifikacijas, PID: 26359
03-13 10:16:45.420 26359 26359 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.notifikacijas/com.notifikacijas.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager com.facebook.react.bridge.ReactApplicationContext.getPackageManager()' on a null object reference
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3107 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3250 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1947 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:106 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:214 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:7032 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Native Method)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager com.facebook.react.bridge.ReactApplicationContext.getPackageManager()' on a null object reference
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at com.notifikacijas.NotificationService.displayNotification(NotificationService.java:34 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at com.notifikacijas.NotificationService.<init>(NotificationService.java:30 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at com.notifikacijas.MainApplication$1.getPackages(MainApplication.java:29 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at com.facebook.react.ReactNativeHost.createReactInstanceManager(ReactNativeHost.java:76 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at com.facebook.react.ReactNativeHost.getReactInstanceManager(ReactNativeHost.java:38 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at com.facebook.react.ReactDelegate.loadApp(ReactDelegate.java:103 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at com.facebook.react.ReactActivityDelegate.loadApp(ReactActivityDelegate.java:88 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at com.facebook.react.ReactActivityDelegate.onCreate(ReactActivityDelegate.java:83 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at com.facebook.react.ReactActivity.onCreate(ReactActivity.java:44 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at android.app.Activity.performCreate(Activity.java:7327 undefined)
03-13 10:16:45.420 26359 26359 E AndroidRuntime:    at andr

MainApplication.java主应用程序

package com.notifikacijas;

import android.app.Application;
import android.content.Context;
import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.soloader.SoLoader;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
// import NotificationService.java;


public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost =
      new ReactNativeHost(this) {
        @Override
        public boolean getUseDeveloperSupport() {
          return BuildConfig.DEBUG;
        }

        @Override
        protected List<ReactPackage> getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List<ReactPackage> packages = new PackageList(this).getPackages();
          // Packages that cannot be autolinked yet can be added manually here, for example:
        new NotificationService(null);
         return packages;
        }

        @Override
        protected String getJSMainModuleName() {
          return "index";
        }
      };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
    initializeFlipper(this); // Remove this line if you don't want Flipper enabled
  }

  /**
   * Loads Flipper in React Native templates.
   *
   * @param context
   */
  private static void initializeFlipper(Context context) {
    if (BuildConfig.DEBUG) {
      try {
        /*
         We use reflection here to pick up the class that initializes Flipper,
        since Flipper library is not available in release mode
        */
        Class<?> aClass = Class.forName("com.facebook.flipper.ReactNativeFlipper");
        aClass.getMethod("initializeFlipper", Context.class).invoke(null, context);
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      } catch (NoSuchMethodException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
    }
  }
}

I think reactContext has null value thats why it is unable to get Package Manager.我认为reactContext具有 null 值,这就是它无法获得包管理器的原因。 try checking that and assign proper object in reactContext.尝试检查并在 reactContext 中分配适当的对象。

Hope this will help you.希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM