我在代码中使用four
字符串。 first
字符串包含“http://
”,second
字符串包含用户主机URL(由用户输入),third
包含“/musics/a.mp3
”和{ {1}}字符串包含完整的URL,通过组合以前的字符串,如下所示:
fourth
然后我在String fourth = first+second+third;
的执行中使用fourth
字符串,如下所示:
AsyncTask
我收到以下错误。
日志:
new JSONAsyncTask().execute(fourth);
活动代码:
10-07 06:17:15.934: E/AndroidRuntime(1194): FATAL EXCEPTION: AsyncTask #1
10-07 06:17:15.934: E/AndroidRuntime(1194): java.lang.RuntimeException: An error occured while executing doInBackground()
10-07 06:17:15.934: E/AndroidRuntime(1194): at android.os.AsyncTask$3.done(AsyncTask.java:278)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-07 06:17:15.934: E/AndroidRuntime(1194): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.lang.Thread.run(Thread.java:856)
10-07 06:17:15.934: E/AndroidRuntime(1194): Caused by: java.lang.IllegalArgumentException: Host name may not be null
10-07 06:17:15.934: E/AndroidRuntime(1194): at org.apache.http.HttpHost.<init>(HttpHost.java:83)
10-07 06:17:15.934: E/AndroidRuntime(1194): at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
10-07 06:17:15.934: E/AndroidRuntime(1194): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-07 06:17:15.934: E/AndroidRuntime(1194): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-07 06:17:15.934: E/AndroidRuntime(1194): at com.app.ifko.HomeActivity$JSONAsyncTask.doInBackground(HomeActivity.java:101)
10-07 06:17:15.934: E/AndroidRuntime(1194): at com.app.ifko.HomeActivity$JSONAsyncTask.doInBackground(HomeActivity.java:1)
10-07 06:17:15.934: E/AndroidRuntime(1194): at android.os.AsyncTask$2.call(AsyncTask.java:264)
10-07 06:17:15.934: E/AndroidRuntime(1194): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-07 06:17:15.934: E/AndroidRuntime(1194): ... 5 more
10-07 06:17:22.764: E/WindowManager(1194): Activity com.app.ifko.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416b80b0 that was originally added here
10-07 06:17:22.764: E/WindowManager(1194): android.view.WindowLeaked: Activity com.app.ifko.HomeActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@416b80b0 that was originally added here
10-07 06:17:22.764: E/WindowManager(1194): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:343)
10-07 06:17:22.764: E/WindowManager(1194): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:245)
10-07 06:17:22.764: E/WindowManager(1194): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:193)
10-07 06:17:22.764: E/WindowManager(1194): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:118)
10-07 06:17:22.764: E/WindowManager(1194): at android.view.Window$LocalWindowManager.addView(Window.java:537)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.Dialog.show(Dialog.java:274)
10-07 06:17:22.764: E/WindowManager(1194): at com.app.ifko.HomeActivity$JSONAsyncTask.onPreExecute(HomeActivity.java:90)
10-07 06:17:22.764: E/WindowManager(1194): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
10-07 06:17:22.764: E/WindowManager(1194): at android.os.AsyncTask.execute(AsyncTask.java:511)
10-07 06:17:22.764: E/WindowManager(1194): at com.app.ifko.HomeActivity.onCreate(HomeActivity.java:74)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.Activity.performCreate(Activity.java:4465)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.ActivityThread.access$600(ActivityThread.java:122)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
10-07 06:17:22.764: E/WindowManager(1194): at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 06:17:22.764: E/WindowManager(1194): at android.os.Looper.loop(Looper.java:137)
10-07 06:17:22.764: E/WindowManager(1194): at android.app.ActivityThread.main(ActivityThread.java:4340)
10-07 06:17:22.764: E/WindowManager(1194): at java.lang.reflect.Method.invokeNative(Native Method)
10-07 06:17:22.764: E/WindowManager(1194): at java.lang.reflect.Method.invoke(Method.java:511)
10-07 06:17:22.764: E/WindowManager(1194): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-07 06:17:22.764: E/WindowManager(1194): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-07 06:17:22.764: E/WindowManager(1194): at dalvik.system.NativeStart.main(Native Method)
10-07 06:17:24.144: I/Process(1194): Sending signal. PID: 1194 SIG: 9
答案 0 :(得分:2)
如果URL的主机部分来自用户,则必须在使用它构建URL之前对其进行验证。
如果new JSONAsyncTask().execute(fourth);
为空,请不要致电second
。
答案 1 :(得分:1)
如果您在etTextToSave.getText()
中拨打onCreate()
,则返回的文字将是布局中默认的内容,而不是用户输入的内容。
代码无法显示您是否正在使用onclick侦听器或类似内容,但如果您使用,则将getText()
移至点击处理程序。