java Android:调用新意图时出现空指针异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31059390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Null Pointer Exception when calling new intent
提问by Akash Magoon
I am receiving an error when trying to call new activity through an intent:
尝试通过意图调用新活动时收到错误消息:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
My Activity has a button, and when clicked, it goes to this method:
我的 Activity 有一个按钮,当点击它时,它转到这个方法:
public void onClickChangeActivity() {
Intent intent = new Intent(context , Details.class);
startActivity(intent);
}
public static Context context is created in the beginning of the class and then is instantiated in my onCreate as context = getApplicationContext();
公共静态上下文上下文在类的开头创建,然后在我的 onCreate 中实例化为 context = getApplicationContext();
I've also tried context = getBaseContext() and context = RecyclerViewActivity.this (RecyclerViewActivity is the name of my current class)
我也试过 context = getBaseContext() 和 context = RecyclerViewActivity.this (RecyclerViewActivity 是我当前类的名称)
Any help as to why I'm receiving this NullP? I've tried everything related StackOverFlow answers have offered. Also, my Manifest includes my activity.
关于我为什么收到这个 NullP 有什么帮助吗?我已经尝试了所有相关 StackOverFlow 答案提供的内容。此外,我的清单包括我的活动。
onClickChangeActivity is being called from my RecyclerViewerAdapter class:
正在从我的 RecyclerViewerAdapter 类调用 onClickChangeActivity:
View.OnClickListener getPersonClickListener(final int position) {
return new View.OnClickListener() {
@Override
public void onClick(View view) {
RecyclerViewActivity recyclerViewActivity = new RecyclerViewActivity();
recyclerViewActivity.onClickChangeActivity();
}
};
Manifest File:
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.akash.android_test" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- <activity -->
<!-- android:name=".DataFetcher" -->
<!-- android:label="@string/title_activity_data_fetcher" > -->
<!-- </activity> -->
<activity
android:name=".Crime"
android:label="@string/title_activity_crime"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".RecyclerViewActivity"
android:label="@string/title_activity_recycler_view"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".Details"
android:label="@string/title_activity_crime_details"
android:parentActivityName=".RecyclerViewActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="RecyclerViewActivity" />
</activity>
</application>
</manifest>
LogCat:
日志猫:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:132)
at android.content.ComponentName.<init>(ComponentName.java:77)
at android.content.Intent.<init>(Intent.java:4160)
at com.bah.baltimoreopendata.android_baltimoreopendata.RecyclerViewActivity.onClickChangeActivity(RecyclerViewActivity.java:265)
at com.bah.baltimoreopendata.android_baltimoreopendata.RecycleViewAdapter.onClick(RecycleViewAdapter.java:121)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
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:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
回答by Morales Batovski
Problem is that you are trying to call an activitymethod from the adapter:
问题是您正在尝试从适配器调用活动方法:
RecyclerViewActivity recyclerViewActivity = new RecyclerViewActivity();
recyclerViewActivity.onClickChangeActivity();
You should replace this code in adapter class. In the adapter constructoradd the following lines:
您应该在适配器类中替换此代码。在适配器构造函数中添加以下几行:
public RecycleViewAdapter(......, Context context){
...your code.
this.mContext=context;
}
And in In the getPersonClickListeneradapter method:
并在getPersonClickListener适配器方法中:
View.OnClickListener getPersonClickListener(final int position) {
return new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mContext instanceof RecyclerViewActivity){
((RecyclerViewActivity)mContext).onClickChangeActivity();
}
}
};
And when you invoke onClickChangeActivitymethod on RecyclerViewActivity use:
当您在 RecyclerViewActivity 上调用onClickChangeActivity方法时,请使用:
public void onClickChangeActivity() {
Intent intent = new Intent(RecyclerViewActivity.this, Details.class);
startActivity(intent);
}
And be sure that Details.classextends Activity.
并确保Details.class扩展了 Activity。
回答by ramin eftekhari
try this:
试试这个:
Intent intent = new Intent(RecyclerViewActivity.this , Details.class);
startActivity(intent);
use RecyclerViewActivity.this
instead of context
使用RecyclerViewActivity.this
代替context
回答by Akash Magoon
Solution:
解决方案:
In the adapter class create a private static Context context;
在适配器类中创建一个 private static Context context;
In your inCreateViewHolder(), instantiate context to viewGroup.getContext():
在您的 inCreateViewHolder() 中,将上下文实例化为 viewGroup.getContext():
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v);
context = viewGroup.getContext();
return pvh;
}
This is the reason I was getting a Null Pointer Exception. For some reason, what I create a constructor with a context, context was still null. Instantiating context in the above class did the trick! Thanks to all who answered.
这就是我收到空指针异常的原因。出于某种原因,我创建一个带有上下文的构造函数,上下文仍然为空。在上面的类中实例化上下文就成功了!感谢所有回答的人。
回答by Ofir Ohayon
You are trying to start Details.class -
您正在尝试启动 Details.class -
But I can not find any references to this activity in your manifest.
但是我在您的清单中找不到任何对此活动的引用。
Please add
请加
<activity android:name=".Details"</activity>
To your manifest and make sure that Details extends Activity.
到您的清单并确保详细信息扩展了活动。