这种情况不常发生,但我在Google Play控制台上收到了2条报告。这是堆栈跟踪:
java.lang.RuntimeException: Unable to start activity ComponentInfo{ru.nikeabooks.eastermessage/ru.nikeabooks.eastermessage.BookActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
at android.app.ActivityThread.access$600(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4822)
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:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at ru.nikeabooks.eastermessage.BookPageFragment.onCreate(BookPageFragment.java:66)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1455)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
at android.support.v4.app.FragmentManagerImpl.dispatchCreate(FragmentManager.java:1872)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:215)
at ru.nikeabooks.eastermessage.BookActivity.onCreate(BookActivity.java:63)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
... 11 more
这是代码(其中一些):
public class BookPageFragment extends Fragment {
SharedPreferences sPref;
//....some more code
static BookPageFragment newInstance(int page) {
BookPageFragment pageFragment = new BookPageFragment();
Bundle arguments = new Bundle();
arguments.putInt(ARGUMENT_PAGE_NUMBER, page);
pageFragment.setArguments(arguments);
return pageFragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageNumber = getArguments().getInt(ARGUMENT_PAGE_NUMBER);
context=BookActivity.getContext();
sPref=context.getSharedPreferences("ru.xxxxxx.xxxx.PREF", context.MODE_PRIVATE);
//and more
因此,sPref=context.getSharedPreferences("ru.xxxxxx.xxxx.PREF", context.MODE_PRIVATE);
行有时会导致崩溃。在大多数时候它运作良好。
任何想法??
答案 0 :(得分:0)
BookActivity.getContext();
看起来很可疑。试试这个
getActivity().getApplicationContext();