【问题标题】:Null Pointer exception when trying to layout inflate in service尝试在服务中布局膨胀时出现空指针异常
【发布时间】:2013-04-21 21:56:41
【问题描述】:

我正在尝试使用以下代码在 onCreate 上的服务中布局 Inflate:

// ...
public void onCreate() {    
    telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    call = new PhoneCall();

    IntentFilter intentFilter = new IntentFilter(outgoingIntent);
    registerReceiver(callReceiver, intentFilter);

    final LayoutInflater myInflator = (LayoutInflater) ctext.getSystemService(Context.LAYOUT_INFLATER_SERVICE );        

    onPhoneStatelistener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

// ...

这是我的日志猫:

04-22 02:38:35.324: E/AndroidRuntime(28837): FATAL EXCEPTION: main
04-22 02:38:35.324: E/AndroidRuntime(28837): java.lang.RuntimeException: Unable to create service com.castello.AllMyCalls.CallService: java.lang.NullPointerException
04-22 02:38:35.324: E/AndroidRuntime(28837):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:1959)
04-22 02:38:35.324: E/AndroidRuntime(28837):    at android.app.ActivityThread.access$2500(ActivityThread.java:117)
04-22 02:38:35.324: E/AndroidRuntime(28837):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:989)

我刚刚收到“空指针”错误。有什么建议吗?

【问题讨论】:

  • 请发布显示 NPE 的 logcat,并指出您的代码中涉及哪一行。
  • 你从哪里得到null?你的contextnull是吗?
  • 更重要的是,您为什么要尝试在服务中扩展布局?服务对膨胀的布局没有任何作用。
  • @TedHopp 我添加了我的 LogCat。
  • @CommonsWare 我想在拨出电话结束时显示自定义 Toast,所以我必须这样做,有什么建议吗?

标签: android service nullpointerexception layout-inflater


【解决方案1】:

当我尝试让LayoutInflater 投入使用时,我通常会这样做:

LayoutInflater inflater = LayoutInflater.from(Context);

希望这对你有用!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    相关资源
    最近更新 更多