【问题标题】:Onclick button crashes application [closed]Onclick按钮使应用程序崩溃[关闭]
【发布时间】:2013-03-10 08:25:34
【问题描述】:

我正在添加一个按钮,当用户单击它时,它会返回主屏幕,但是每次单击该按钮时应用程序都会强制关闭。

Source Code

【问题讨论】:

标签: java android eclipse


【解决方案1】:

我猜你正在获得 NPE。

PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);  <<<<Error is here NPE

Intent intent = new Intent();
Intent intent=new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);  <<<There must be Compile time error because you did not declare homeIntent
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeIntent);

【讨论】:

    【解决方案2】:

    就我所见,我认为您的问题在这里:

    public void onClick(View v) {
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
    Notification noti = new Notification.Builder(this)
    .setTicker("Ticker Title")
    .setContentTitle("Content Title")
    .setContentText("Notifcation Content")
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentIntent(pIntent).getNotification();
    noti.flags=Notification.FLAG_AUTO_CANCEL;
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0, noti);
    
    
    Intent intent = new Intent();
    Intent intent=new Intent(Intent.ACTION_MAIN);
    homeIntent.addCategory(Intent.CATEGORY_HOME);
    homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(homeIntent);
    }
    

    以及更准确的这一行的意图:

    PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
    

    您正试图在实际创建之前将此意图添加到 PendingIntent

    您使用的homeIntent 是什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多