getPackageManager()正在获取NullPointer异常

时间:2015-12-15 15:12:03

标签: java android nullpointerexception android-context android-package-managers

我从mainactivity将上下文传递给AppInfo类,然后使用它尝试从AppInfo类访问packagemanager,但是我得到空指针异常。

MainActivity类:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener{

    Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     ....
     ....
        new AppInfo(this);
    }
}

AppInfo类:

public class AppInfo  {

    Context mContext;

    AppInfo(Context context)
    {
        this.mContext = context;
    }

    PackageManager pm = mContext.getPackageManager();

日志:

致命的例外:主要

处理:com.example.zahidhasan.myapplication,PID:12255                                                                                   java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.zahidhasan.myapplication / com.example.zahidhasan.uninstaller.MainActivity}:java.lang.NullPointerException:尝试调用虚方法' android.content。 pm.PackageManager android.content.Context.getPackageManager()'在null对象引用上                                                                                       在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)

0 个答案:

没有答案