[英]Caused by: java.lang.IllegalArgumentException: Host name may not be null
I am using four
Strings in my code. 我在代码中使用了four
字符串 。 The first
String contains " http://
", the second
String contains user host URL (entered by user), the third
contains " /musics/a.mp3
" and the fourth
String contains complete URL, by combining the previous Strings, like this: 通过组合first
字符串, first
字符串包含“ http://
”, second
字符串包含用户主机URL(由用户输入), third
fourth
字符串包含“ /musics/a.mp3
”, fourth
字符串包含完整的URL,例如这个:
String fourth = first+second+third;
Then I'm using the fourth
String in AsyncTask
's execute, like this: 然后,我在AsyncTask
的execute中使用fourth
String,如下所示:
new JSONAsyncTask().execute(fourth);
And I'm getting the error below. 我在下面看到错误。
Log: 日志:
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
Activity code: 活动代码:
public class HomeActivity extends Activity {
String first, second, third, fourth;
TextView etTextToSave;
private SharedPreferences sharedprefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
etTextToSave = (TextView) findViewById(R.id.etTextToSave);
sharedprefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
etTextToSave.setText(sharedprefs.getString("SharedPrefsData",""));
second = etTextToSave.getText().toString();
first = "http://";
third = "/musics/a.mp3";
fourth = first+second+third;
.........................
new JSONAsyncTask().execute(fourth);
}
If the host part of the URL comes from the user, you must validate it before using it to construct a URL. 如果URL的主机部分来自用户,则必须在使用URL构造URL之前对其进行验证。
Don't call new JSONAsyncTask().execute(fourth);
不要调用new JSONAsyncTask().execute(fourth);
if second
is null. 如果second
为null。
If you're calling etTextToSave.getText()
in onCreate()
, the returned text will be whatever there's by default in the layout and not what was entered by the user. 如果要在onCreate()
调用etTextToSave.getText()
,则返回的文本将是布局中默认存在的内容,而不是用户输入的内容。
The code doesn't show if you're using an onclick listener or similar but if you are, move the getText()
to the click handler. 如果您使用的是onclick侦听器或类似的侦听器,则不会显示代码,但如果使用的话,请将getText()
移至单击处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.