【问题标题】:Calling Status Bar notification from method from other class从其他类的方法调用状态栏通知
【发布时间】:2011-06-08 19:35:28
【问题描述】:

首先,我对 android 和 Java 都是新手。

我有两个类,我的 main.class 和 Note.class。

当我按下按钮时,我在 main.class 中的 Note.class 中调用通知方法。

问题在于 Note.class 中的这一行:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

当方法被调用时,它会强制关闭。我认为问题出在 PendingIntent.getActivity(this, 0, notificationIntent, 0); 中的“this”,但我不确定将其更改为什么。

如果它在主类中,通知代码可以正常工作。

我将非常感谢任何指导。

编辑: 主类:http://pastebin.com/05Yx0a48

Note.class:

package com.adamblanchard.remindme.com.adamblanchard;

import com.adamblanchard.remindme.R;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;



public class Note extends Activity {

public CharSequence note = "not changed";
int HELLO_ID = 1;

/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    setTitle("Remind Me!");


}

//Notification Method

public void callNotification() {
    // TODO Auto-generated method stub


    String ns = Context.NOTIFICATION_SERVICE;
    final NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

    int icon = R.drawable.launcher;
    CharSequence tickerText = "Remind Me!";
    long when = System.currentTimeMillis();

    final Notification notification = new Notification(icon, tickerText, when);

notification.flags |= Notification.FLAG_AUTO_CANCEL;

final Context context = getApplicationContext();
CharSequence contentTitle = "Remind Me!";
CharSequence contentText = note;

Intent notificationIntent = new Intent(context, AndroidNotifications.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);





        mNotificationManager.notify(HELLO_ID, notification);

        HELLO_ID++;
}

 }

调试输出:

线程 [ main](挂起(异常 IllegalStateException))
Note(Activity).getSystemService(String) 行:3536
Note.callNotification() 行:37
提醒我$1$1.onClick(DialogInterface, int) 行:72 AlertDialog(AlertController$ButtonHandler).handleMessage(Message) 行:159 AlertController$ButtonHandler(Handler).dispatchMessage(Message) 行:99
Looper.loop() 行:123 ActivityThread.main(String[]) 行:3647
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) 行:不可用 [本机方法]
Method.invoke(Object, Object...) 行:507
ZygoteInit$MethodAndArgsCaller.run() 行:839
ZygoteInit.main(String[]) 行:597 NativeStart.main(String[]) 行:不可用 [本机方法]

这是我得到的调试输出,加上设备上的强制关闭弹出窗口。

编辑2:

清单 xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.adamblanchard.remindme"
  android:versionCode="3"
  android:versionName="0.7">
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher72">

<activity android:name=".com.adamblanchard.remindme" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
</activity>

<activity android:name=".Note">
<intent-filter>
<action android:name="Note" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>


<uses-sdk android:minSdkVersion="1"></uses-sdk>
</manifest> 

堆栈跟踪(这些是你的意思吗?):

Thread [<1> main] (Suspended (exception ActivityNotFoundException)) 
Instrumentation.checkStartActivityResult(int, Object) line: 1404    
Instrumentation.execStartActivity(Context, IBinder, IBinder, Activity, Intent, int) line: 1378  
remindme(Activity).startActivityForResult(Intent, int) line: 2827   
remindme(Activity).startActivity(Intent) line: 2933 
remindme$1$1.onClick(DialogInterface, int) line: 82 
AlertDialog(AlertController$ButtonHandler).handleMessage(Message) line: 159 
AlertController$ButtonHandler(Handler).dispatchMessage(Message) line: 99    
Looper.loop() line: 123 
ActivityThread.main(String[]) line: 3647    
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
Method.invoke(Object, Object...) line: 507  
ZygoteInit$MethodAndArgsCaller.run() line: 839  
ZygoteInit.main(String[]) line: 597 
NativeStart.main(String[]) line: not available [native method]  

或:

01-15 00:56:18.167: WARN/dalvikvm(14887): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-15 00:56:18.217: ERROR/AndroidRuntime(14887): FATAL EXCEPTION: main
01-15 00:56:18.217: ERROR/AndroidRuntime(14887): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.adamblanchard.remindme/com.adamblanchard.remindme.com.adamblanchard.Note}; have you declared this activity in your AndroidManifest.xml?
01-15 00:56:18.217: ERROR/AndroidRuntime(14887):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
01-15 00:56:18.217: ERROR/AndroidRuntime(14887):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
01-15 00:56:18.217: ERROR/AndroidRuntime(14887):     at android.app.Activity.startActivityForResult(Activity.java:2827)
01-15 00:56:18.217: ERROR/AndroidRuntime(14887):     at android.app.Activity.startActivity(Activity.java:2933)
01-15 00:56:18.217: ERROR/AndroidRuntime(14887):     at com.adamblanchard.remindme.com.adamblanchard.remindme$1$1.onClick(remindme.java:82)
01-15 00:56:18.217: ERROR/AndroidRuntime(14887):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
01-15 00:56:18.217: ERROR/AndroidRuntime(14887):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-15 00:56:18.217: ERROR/AndroidRuntime(14887):     at android.os.Looper.loop(Looper.java:123)
01-15 00:56:18.217: ERROR/AndroidRuntime(14887):     at android.app.ActivityThread.main(ActivityThread.java:3647)
01-15 00:56:18.217: ERROR/AndroidRuntime(14887):     at java.lang.reflect.Method.invokeNative(Native Method)
01-15 00:56:18.217: ERROR/AndroidRuntime(14887):     at java.lang.reflect.Method.invoke(Method.java:507)
01-15 00:56:18.217: ERROR/AndroidRuntime(14887):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-15 00:56:18.217: ERROR/AndroidRuntime(14887):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-15 00:56:18.217: ERROR/AndroidRuntime(14887):     at dalvik.system.NativeStart.main(Native Method)
01-15 00:56:18.237: WARN/ActivityManager(156):   Force finishing activity com.adamblanchard.remindme/.com.adamblanchard.remindme
01-15 00:56:18.747: WARN/ActivityManager(156): Activity pause timeout for HistoryRecord{40a15868 com.adamblanchard.remindme/.com.adamblanchard.remindme}
01-15 00:56:18.777: DEBUG/Launcher(10740): -- loadPreferences()
01-15 00:56:19.177: INFO/ActivityManager(156): No longer want com.facebook.katana (pid 14395): hidden #16
01-15 00:56:29.177: WARN/ActivityManager(156): Activity destroy timeout for HistoryRecord{40a15868 com.adamblanchard.remindme/.com.adamblanchard.remindme}

【问题讨论】:

  • 你能给我们看看你的 Note 课上的代码吗?另外,请向我们展示您收到的错误消息的详细信息。
  • “main”和“Note”都是活动吗?发布更多详细信息将帮助我们回答您的问题。

标签: java android


【解决方案1】:

好的!我修好了!

在 manifest.xml 中,我需要使用活动的全名,即:

<activity android:name=".com.adamblanchard.Note"></activity>

我一直在搞乱代码,所以可能有其他的东西有影响,但是一旦我改变它就可以了。

【讨论】:

  • 您应该将活动包的名称(在您的 Java 代码中)更改为 com.adamblanchard 而不是 com.adamblanchard.remindme.com.adamblanchard 。后者作为包名没有意义。
  • 完成。我不确定这是怎么发生的。
【解决方案2】:

在您的提醒活动中,您有以下行: Note Note = new Note();

这不是实例化Activity不正确方法。你应该创建一个Intent

Intent myIntent = new Intent(this, Note.class);

并将其传递给startActivity

【讨论】:

  • 我尝试过使用 Intent myIntent = new Intent(remindme.this, Note.class);提醒我.this.startActivity(myIntent);当按下按钮时,但我仍然强制关闭。我相信我需要在 androidmanifest 中声明意图,但我不确定该放什么。谢谢你的帮助。
  • 您需要在清单中声明所有活动。例如:
  • 这就是我现在所拥有的,我仍然在按下按钮时强制关闭。
  • 发布整个 manifest.xml。还要发布你得到的异常的堆栈跟踪,它应该在你的 LogCat 窗口中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-15
  • 1970-01-01
  • 2013-01-12
  • 1970-01-01
相关资源
最近更新 更多