Question

I write a simple app that if when I press the button a facebook notification set one variable to a specific values, When I run this code on my phone the app go on crash, and before that I see one error in this line (sbn.getPackageName().contains("com.facebook.katana") telling me that API 18 is required, but I've already added it in the manifest! I don't know if this can help, but I'm using Android Studio!

In the MainActivity

 public class MainActivity extends ActionBarActivity {
    private static int i=0;

    private  StatusBarNotification sbn;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        Button b1,b2,b3,b4;
        b1 = (Button) findViewById(R.id.button);
        b2= (Button) findViewById(R.id.button2);
        b3= (Button) findViewById(R.id.button3);
        b4= (Button) findViewById(R.id.button4);


       b1.setOnClickListener(new View.OnClickListener()
                {
                    public void onClick(View v)
                    {


                        if(sbn.getPackageName().contains("com.facebook.katana")) {
                            i = 1;
                            Toast msg;
                            msg = Toast.makeText(getBaseContext(),
                                    "IT'S GREEEEEN!", Toast.LENGTH_LONG);
                            msg.show();
                        }
                    }
                });

In the AndroidManifest file

<uses-sdk
                android:minSdkVersion="18"
                android:targetSdkVersion="18" />

Logcat

05-01 15:43:32.914  30717-30717/com.example.myapplication.app D/AndroidRuntime﹕ Shutting down VM
05-01 15:43:32.914  30717-30717/com.example.myapplication.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4160cba8)
05-01 15:43:32.914  30717-30717/com.example.myapplication.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.myapplication.app, PID: 30717
    java.lang.NullPointerException
            at com.example.myapplication.app.MainActivity$1.onClick(MainActivity.java:49)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
Was it helpful?

Solution

Issue is that sbn is null, in your onClick

You need to get the list of notification with NotificationListenerService, before accessing it.

check this https://developer.android.com/reference/android/service/notification/NotificationListenerService.html

(sbn.getPackageName().contains("com.facebook.katana") telling me that API 18 is required

You need to change minSdkVersion 18 in gradle build config

OTHER TIPS

Android Studio does not read the minimum SDK level from AndroidManifest.xml file. It reads it from build.gradle file. Your build.gradle file should contain these lines below:

defaultConfig {
    minSdkVersion 18
    targetSdkVersion 18
    versionCode 1
    versionName "1.0"
}

Your app is crashing because sbn variable is only declared and never initialized.

That means you will have a NullPointerException when using it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top