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

Java URLEncoder使我的应用程序崩溃(实际上是一个过早初始化的变量导致了崩溃),java,android,urlencode,Java,Android,Urlencode,我知道,我是这里的询问型用户。。。好 此代码使我的应用程序崩溃,它是对话框按钮的一部分。方法内部 try { eq = URLEncoder.encode(q, "UTF-8"); } catch (UnsupportedEncodingException e)

我知道,我是这里的询问型用户。。。好 此代码使我的应用程序崩溃,它是对话框按钮的一部分。方法内部

                    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);
如果我只输入了意图,不使用eq,不执行urlcoder,代码中根本没有它,那么它就可以工作。有人知道我做错了什么吗?q中有测试搜索%s,在我的情况下,它将被GT-I9100设备型号替换,然后变量eq应该是test+search+GT-I9100或类似的内容

我的日志说:

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
从stacktrace:

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

在创建活动生命周期之前,您不能访问资源。构造阶段,例如初始化成员变量太早。

我们可以看到异常的堆栈跟踪吗?@Fustigador等等,我会用它编辑我的帖子。我宁愿打赌,这可能与varargs%s有关,但堆栈跟踪有望显示原因。可能格式是在URL编码之后完成的,这本身可能会引入“%XX”@JoopEggen No,这不是原因,因为我使用的是getStringR.string.searchquery1,android.os.Build.MODEL;,如果我删除URLEncoder,它会工作。我无法获得堆栈跟踪。那太糟糕了。e、 getStackTrace没有执行…换句话说,在onCreate中执行您的操作,而不是在activity.Oh的构造函数中。我尝试在与eq方法相同的方法中初始化变量q,现在它可以工作了。我真的忽略了最简单的事情,我真的尝试在onCreate方法之前初始化它。非常感谢。