来自通知服务的dispatchKeyEvent

时间:2016-08-09 21:29:53

标签: java android nullpointerexception

我正在构建一个自定义通知用户界面,它有一个后退按钮,一个主页按钮和一个列出所有正在运行的应用程序的按钮(类似于屏幕导航栏,但这个在通知面板中)

在后退按钮上单击我有以下代码

private void onBackClick() {
    dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
    dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
}

但每当我点击通知栏中的后退按钮时,我会得到例外,如下所示

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test.sunshine/com.example.test.actions.NotificationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.android.internal.policy.PhoneWindow$DecorView.superDispatchKeyEvent(android.view.KeyEvent)' on a null object reference
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
                                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.android.internal.policy.PhoneWindow$DecorView.superDispatchKeyEvent(android.view.KeyEvent)' on a null object reference
                                                                                     at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1777)
                                                                                     at android.app.Activity.dispatchKeyEvent(Activity.java:2731)
                                                                                     at com.example.test.actions.NotificationActivity.onBackClick(NotificationActivity.java:49)
                                                                                     at com.example.test.actions.NotificationActivity.onCreate(NotificationActivity.java:23)
                                                                                     at android.app.Activity.performCreate(Activity.java:6251)
                                                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510) 
                                                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:148) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5461) 
                                                                                     at java.lang.reflect.Method.invoke(Native Method)

我使用以下代码

从我的主要活动开始服务
private void showNotification() {

    RemoteViews views = new RemoteViews(getPackageName(),
            R.layout.notification_bar);

    Intent mainIntent = new Intent(this, Index.class);
    mainIntent.setAction("main");

    Intent homeIntent = new Intent(this, NotificationActivity.class);
    homeIntent.setAction("home");

    Intent backIntent = new Intent(this, NotificationActivity.class);
    backIntent.setAction("back");

    Intent listIntent = new Intent(this, NotificationActivity.class);
    listIntent.setAction("list");

    views.setOnClickPendingIntent(R.id.welcome, PendingIntent.getActivity(getApplicationContext(),
            0, homeIntent, 0));
    views.setOnClickPendingIntent(R.id.back, PendingIntent.getActivity(getApplicationContext(),
            0, backIntent, 0));
    views.setOnClickPendingIntent(R.id.list, PendingIntent.getActivity(getApplicationContext(),
            0, listIntent, 0));

    Notification notification =
            new NotificationCompat.Builder(this)
                    .setContent(views)
                    .setSmallIcon(R.drawable.notification_icon)
                    .setOngoing(true)
                    .setPriority(Notification.PRIORITY_MIN)
                    .build();

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, notification);
}

和通知活动类如下所示

public class NotificationActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String action = getIntentAction(getIntent());
        switch (action) {
            case "home":
                onHomeClick();
                break;
            case "back":
                onBackClick();
                break;
            case "list":
                onListClick();
                break;
            default:
                break;
        }

        moveTaskToBack(true);
    }

    /**
     * methid called when home button is clicked
     */
    private void onHomeClick() {
        Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);
    }

    /**
     * method called when back button is clicked
     */
    private void onBackClick() {
        this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
        this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
    }

    /**
     * method called when list button is clicked
     */
    private void onListClick() {
        KeyEvent kTab = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_TAB);
        dispatchKeyEvent(kTab);
    }

    /**
     * method to get action parameter as passed to Intent
     * @return {@link String}
     */
    private String getIntentAction(Intent intent) {

        if (intent == null) {
            return "";
        }

        return intent.getAction();
    }
}

我这样做是为了更多的实验,而现在除了 home 按钮之外似乎没有任何效果。任何有助于工作的帮助都会很棒

0 个答案:

没有答案