【问题标题】:Android java.lang.NullPointerException error with BroadcastReceiverAndroid java.lang.NullPointerException 错误与 BroadcastReceiver
【发布时间】:2020-07-01 13:24:03
【问题描述】:

每当应用程序进入我的第二个活动时,它就会崩溃,并给出错误。

我的活动:

public class SecondActivity extends AppCompatActivity {

    EditText barcodeText = findViewById(R.id.barcodeText);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        IntentFilter filter = new IntentFilter();
        filter.addCategory(Intent.CATEGORY_DEFAULT);
        filter.addAction(getResources().getString(R.string.activity_intent_filter_action));
        registerReceiver(myBroadcastReceiver, filter);
    }

    private BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Bundle b = intent.getExtras();

            if (action.equals(getResources().getString(R.string.activity_intent_filter_action))) {
                try {
                    displayScanResult(intent);
                } catch (Exception e) {
                }
            }
        }
    };

    private void displayScanResult(Intent initiatingIntent)
    {
        String decodedSource = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_source));
        String decodedData = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_data));
        String decodedLabelType = initiatingIntent.getStringExtra(getResources().getString(R.string.datawedge_intent_key_label_type));

        barcodeText.setText(decodedData);
    }
}

Logcat:

07-01 12:37:03.373 349-349/com.example.provatimer E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.provatimer, PID: 349
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.provatimer/com.example.provatimer.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5256)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:149)
        at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:99)
        at android.content.Context.obtainStyledAttributes(Context.java:438)
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:692)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:659)
        at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:479)
        at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:214)
        at com.example.provatimer.SecondActivity.<init>(SecondActivity.java:14)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.Class.newInstance(Class.java:1606)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5256) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 

我认为这可能与 BroadcastReceiver 中的上下文有关,但我尝试在 onCreate 方法中声明它,但没有任何改变。 也许我没有正确初始化应该存储数据的意图,如果是这样,我该如何正确地做到这一点? string.xml 文件中的所有字符串都应该是正确的,如果错误可能来自我会写的。

【问题讨论】:

  • 崩溃指向哪一行?
  • @Shrikant 没有说在哪一行
  • 好的,你从哪里发送这个广播?因为我看不出你的代码有任何问题。
  • 它应该通过 DataWedge 应用程序从运行应用程序的设备上的扫描仪发送

标签: java android datawedge


【解决方案1】:

我认为错误发生在这里:

EditText barcodeText = findViewById(R.id.barcodeText);

您正在类成员声明中直接调用findViewById()

你在setContentView()之后调用了findViewById()

EditText barcodeText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    barcodeText = findViewById(R.id.barcodeText);
}

【讨论】:

  • 我不敢相信这是一个简单的错误。现在完美运行,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
  • 2013-11-07
  • 1970-01-01
相关资源
最近更新 更多