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

时间:2015-08-03 09:32:06

标签: java android urlencode

我知道,我是这里的“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);

如果我只将“http://www.google.com”放在Intent中,并且不使用eq并且不执行URLEncoder(根本没有代码),它就可以了。有谁知道我做错了什么? q有“测试搜索%s,它被设备模型(在我的情况下是GT-I9100)取代,然后变量eq应该是”test + search + GT-I9100“或类似的东西。< / p>

我的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.access$900(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

1 个答案:

答案 0 :(得分:4)

来自stacktrace:

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

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