我在Android versions 4.+
中遇到了此错误(特别是在4.1和4.3中(,而在任何其他版本的Android中都没有。活动和库(我使用 GooglePlayServices
和 YouTubePlayerAPI
(在 Manifest.xml 文件和 gradle 文件中引用。
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.test/com.app.test.DetailActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.app.test.content.FirstFragment.onCreate(FirstFragment.java:151)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1912)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:266)
at com.app.test.DetailActivity.onCreate(DetailActivity.java:73)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
... 11 more
导致错误的片段(第 151 行(中的代码是附加片段的 onCreate(( 方法:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
if(getArguments() != null && getArguments().containsKey(ARG_ITEM_ID)) {
// Loads the content specified by the fragment arguments.
mItem = Items.getInstance().getMap().get(getArguments().getString(ARG_ITEM_ID));
}
// Initialize mCustomMediaPlayer
mCustomMediaPlayer = new CustomMediaPlayer(getActivity().getApplicationContext(), mItem.getTrackId());
}
其中,尖线 151 是:
mCustomMediaPlayer = new CustomMediaPlayer(VillancicosActivity.getContext(), mItem.getTrackId());
自定义媒体播放器类:
public class CustomMediaPlayer {
/////// Constants
private static final String TAG = "CustomMediaPlayer";
/////// Fields
/**
* The application context.
* Is used to manage MediaPlayer and async class.
*/
private Context mContext;
/**
* MediaPlayer instance.
*/
private MediaPlayer mMediaPlayer;
/**
* The id representing track used in MediaPlayer create() method
* where will be played.
*/
private Integer mTrackId;
/**
* A flag indicating if the track is playing or not.
*/
private Boolean mIsPlaying = false;
/**
* Initialize providing the application context and the calling fragment track id.
*
* @param context The Context to use.
* @param trackId The raw resource id to use as datasource.
*/
public CustomMediaPlayer(Context context, Integer trackId) {
mContext = context;
mTrackId = trackId;
}
.....
}
这是类的一部分,其中主构造函数是从第 151 行的 FirstFragment 调用的。
知道吗?谢谢大家!
试试这个
mCustomMediaPlayer = new CustomMediaPlayer(getActivity(), mItem.getTrackId());
并检查是否mItem ==null
。
您的活动无法识别Villancicos Activity.getContext((,则需要传递有效的上下文。尝试 getApplicationContext(( 代替。