URLEncoder 使我的应用程序崩溃(这实际上是一个过早初始化的变量导致崩溃)

URLEncoder crashes my app (It was actually a too early initialized variable causing the crash)

我知道,我是这里的 "Asker" 类型的用户...嗯... 此代码使我的应用程序崩溃,它是对话框按钮的一部分。 (方法内部)

                    try
                    {
                        eq = URLEncoder.encode(q, "UTF-8");
                    }
                    catch (UnsupportedEncodingException e)
                    {throw new AssertionError("Your system is messed up. UTF-8 is always a supported encoder, but it failed on your system.",e);}

不管我把它放在哪里,我的应用程序都会在启动时崩溃。 这些代码行初始化

public String eq;

然后我这样做:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/search?q=" + eq));
                    startActivity(browserIntent);

如果我只在 Intent 中放入“http://www.google.com”,而忽略 eq 并且不执行 URLEncoder(代码中根本没有它),它就可以工作。有谁知道我做错了什么? q 中有 "test search %s in it, which is getting replaced with the device model (GT-I9100 in my case) and then the variable eq should be "test+search+GT-I9100" 或类似的内容。

我的 Logcat 说:

java.lang.RuntimeException: Unable to   
instantiate activity ComponentInfo{com.mm.rootchecker/com.mm.rootchecker.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2421)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at android.app.ActivityThread.access0(ActivityThread.java:153)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at android.os.Handler.dispatchMessage(Handler.java:102)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at android.os.Looper.loop(Looper.java:135)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at android.app.ActivityThread.main(ActivityThread.java:5347)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Native Method)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Method.java:372)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:115)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at android.content.ContextWrapper.getResources(ContextWrapper.java:86)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at android.content.Context.getString(Context.java:390)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at com.mm.rootchecker.MainActivity.<init>(MainActivity.java:128)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at java.lang.reflect.Constructor.newInstance(Native Method)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at java.lang.Class.newInstance(Class.java:1606)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
08-03 11:47:20.242 20099 20099 E   AndroidRuntime                               ... 11 more

来自堆栈跟踪:

  at android.content.Context.getString(Context.java:390)
  at com.mm.rootchecker.MainActivity.<init>(MainActivity.java:128)

您无法访问 activity 生命周期 onCreate() 之前的资源。施工阶段 <init> 例如初始化成员变量太早了。