简体   繁体   English

Android Studio包名称未解析,无法获取上下文

[英]Android Studio package name not resolved and can't get context

Made an app, wanted to show a toast but again the sh*tty error popped up saying the context cannot be invoked on a null object reference (yes it's in a different void and yes it's running on the ui thread) I tried adding android:name to the application area in the androidmanifest (package name is noahvt.vremote) and vremote was in red and said that the symbol could not be resolved which is really weird as that is the correct name! 做了一个应用程序,想显示一个敬酒,但再次弹出sh * tty错误,说不能在空对象引用上调用上下文(是的,它在另一个void中,是在ui线程上运行),我尝试添加android: androidmanifest中的应用程序区域的名称(程序包名称为noahvt.vremote),vremote用红色表示无法解析该符号,这确实很奇怪,因为这是正确的名称!

here is a part of my android manifest: 这是我的Android清单的一部分:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<application
    android:name="noahvt.vremote" <---- vremote is in red
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar" >

And here is the non working show toast void i created: 这是我创建的无法正常工作的烤面包吐司:

 public void Toasts()
{
    final String RecToast = g.GetToast();

    MainActivity.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            final Context context = getApplicationContext();
            Toast.makeText(context , RecToast, Toast.LENGTH_LONG).show();


        }
    });
}

The context is giving a nullpointerexception which leads me to think that it has something to do with the android:name 上下文给出了一个nullpointerexception,这使我认为它与android:name有关

BTW this is the stacktrace: 顺便说一句,这是堆栈跟踪:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:106)
        at noahvt.vremote.MainActivity$3.run(MainActivity.java:169)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5289)
        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)
        at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)

May this link could help you Android: Including multiple Java Packages to Manifest 希望此链接对您有帮助吗? Android:包含多个Java软件包以进行清单

Also check the import statement at the top of your activity. 还要检查活动顶部的导入语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM