【问题标题】:NullPointerException when starting service启动服务时出现 NullPointerException
【发布时间】:2012-06-18 18:25:13
【问题描述】:

您好,我目前正在学习如何在 android 中使用服务和通知,当我尝试启动服务时遇到了 NullPointerException。 我写了一个小应用程序,其中有一个按钮和 2 个 EditTextes(标题和内容)。当我单击按钮时,我检索 EditTextes 中的值并启动一个创建通知的新服务。我的代码分为两部分。这是我的代码。这个展示了应用程序的主要活动:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button = (Button) findViewById(R.id.serviceButton);

    final Context ctx = getApplicationContext();

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent i = new Intent(ctx, NotificationService.class);


            Toast.makeText(ctx, "Clicking ...", Toast.LENGTH_SHORT).show();

            EditText title = (EditText) findViewById(R.id.notif_title);
            EditText content = (EditText) findViewById(R.id.notif_content);

            Bundle bundle = new Bundle();
            bundle.putCharSequence("NOTIF_TITLE", title.getText());
            bundle.putCharSequence("NOTIF_CONTENT", content.getText());
            i.putExtras(bundle);

            startService(i);

        }
    });
}

这个描述了将启动通知的服务:

public class NotificationService extends IntentService {

public NotificationService(String name) {
    super(name);
}

@Override
protected void onHandleIntent(Intent intent) {

    Context ctx = getApplicationContext();
    Toast.makeText(ctx, "NotificationService", Toast.LENGTH_LONG).show();

    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager notificationManager = (NotificationManager) getSystemService(ns);


    Bundle b = intent.getExtras();

    String title = b.getString("NOTIF_TITLE");
    String content = b.getString("NOTIF_CONTENT");
    Intent i = new Intent(this, NotificationService.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, i,
            Intent.FLAG_ACTIVITY_NEW_TASK);

    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    Notification notification = new Notification(icon,
            "New notification arrives", when);

    notification.setLatestEventInfo(ctx, title, content, pendingIntent);

    notificationManager.notify(
            NOTIFICATIONS.HELLO_NOTIFICATION.ordinal(), notification);
}

}

当我点击按钮时,我有这个异常:

    FATAL EXCEPTION: main
 java.lang.NullPointerException 
at  android.content.ContextWrapper.startService(ContextWrapper.java:336)
at com.androidprojects.AndroidActivity$1.onClick(AndroidActivity.java:67)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

FATAL EXCEPTION: main
java.lang.NullPointerException
at android.content.ContextWrapper.startService(ContextWrapper.java:336)
at com.androidprojects.AndroidActivity$1.onClick(AndroidActivity.java:62)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9080)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

编辑:我更正了我的代码,删除了new NotificationService,但我仍然得到NullPointerException

【问题讨论】:

  • 我认为不需要 NotificationService notificationService = new NotificationService("");只需调用您的 startService(intent);
  • .this.startService(i);........

标签: android nullpointerexception android-service


【解决方案1】:

通过将其对象初始化为new 来运行您的服务。只需按如下方式运行您的服务:

startService(i);

欲了解更多信息,请阅读this

【讨论】:

  • 我进行了更正,但仍然有 NullPointerException
  • 尝试将final Context ctx = getApplicationContext(); 更改为final Context ctx = this; 看看是否有效
【解决方案2】:

我终于纠正了我的问题。在我进行了 Terrance、@waqas、@akki 建议的更正后,我遇到了另一个例外:InstantiationException。问题出在我的NotificationService 课堂上。当你扩展IntentService来定义一个新服务时,你必须定义一个默认构造函数,但是这个构造函数不带参数,我们必须在其中调用super(name)
我刚刚删除了 NotificationService 类的构造函数中的参数,一切正常。谢谢你的帮助!!

【讨论】:

    【解决方案3】:

    我认为 akki 是正确的 http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/ 所以只需删除 NotificationService 的实例化。如链接的第三步所示,只需调用 make your intent。

    这样的事情

    
    button.setOnClickListener(new View.OnClickListener() {      
            @Override      
            public void onClick(View v) {      
                startService(i);
    
                Toast.makeText(ctx, "Clicking ...", Toast.LENGTH_SHORT).show();      
    
                EditText title = (EditText) findViewById(R.id.notif_title);      
                EditText content = (EditText) findViewById(R.id.notif_content);      
    
                Bundle bundle = new Bundle();      
                bundle.putCharSequence("NOTIF_TITLE", title.getText());      
                bundle.putCharSequence("NOTIF_CONTENT", content.getText());      
                i.putExtras(bundle);      
    
                notificationService.startService(i);      
    
            }      
        });      
    

    【讨论】:

      猜你喜欢
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多