gpt4 book ai didi

android - Android错误日志, list 中的 Activity ,FATAL EXCEPTION : AsyncTask #1

转载 作者:行者123 更新时间:2023-12-03 08:13:52 27 4
gpt4 key购买 nike

另一个Android问题,我试图将数据从应用程序上的edittext发布到服务器上的脚本,但又一次遇到一个错误。这就是我现在所拥有的

package com.college.slimandsave;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class FoodSearchActivity extends Activity implements OnClickListener{


public ListView list;
public Button btnSearch;
private static EditText txtFoodSearch;
private ProgressBar pb;




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_search);
txtFoodSearch = (EditText)findViewById(R.id.txtFoodSearch);

// txtFoodSearch = txtFoodSearch.getText().toString();

btnSearch = (Button)findViewById(R.id.btnSearch);
// btnSearch.setOnClickListener(new View.OnClickListener() {

pb =(ProgressBar)findViewById(R.id.progressBar1);
pb.setVisibility(View.GONE);
btnSearch.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu); // check this
return true;
}

public void onClick(View v) {

if(txtFoodSearch.getText().toString().length()<1) {

Toast.makeText(this , "please enter something", Toast.LENGTH_LONG).show();
}else{
pb.setVisibility(View.VISIBLE);
new MyAsyncTask().execute(txtFoodSearch.getText().toString());
}

}

private class MyAsyncTask extends AsyncTask<String, Integer, Double>{


String fsearch = txtFoodSearch.getText().toString();

@Override
protected Double doInBackground(String... params) {

postData(params[0]);
return null;
}

protected void onPostExecute(Double result) {
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "search sent", Toast.LENGTH_LONG).show();
}

protected void onProgressUpdate(Integer...progress) {
pb.setProgress(progress[0]);
}

public void postData(String fsearch) {
//create new http client
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("192.168.133.84/SlimandSave/try.php");

try {
//add data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("myHttpData", fsearch));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

//execute HTTP post request
HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {

} catch (IOException e) {

}
}
}

}

我的 list 是这个
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.college.slimandsave"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.college.slimandsave.HomeScreenActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.college.slimandsave.StatsScreenActivity"
android:label="@string/lblStatsScreen" >
</activity>
<activity
android:name="com.college.slimandsave.FoodDiaryActivity"
android:label="@string/lblFoodDiary" >
</activity>
<activity
android:name="com.college.slimandsave.SlimAndSaveActivity"
android:label="@string/Slim_And_Save" >
</activity>
<activity
android:name="com.college.slimandsave.AboutScreenActivity"
android:label="@string/lblAbout" >
</activity>
<activity
android:name="com.college.slimandsave.FoodSearchActivity"
android:label="@string/lblFoodSearch" >
</activity>
<activity
android:name="com.college.slimandsave.GetItemsActivity"
android:label="@string/app_name" >
</activity>


</application>




</manifest>

最后是错误日志。
    02-19 08:49:25.019: E/AndroidRuntime(5574): FATAL EXCEPTION: AsyncTask #1
02-19 08:49:25.019: E/AndroidRuntime(5574): java.lang.RuntimeException: An error occured while executing doInBackground()
02-19 08:49:25.019: E/AndroidRuntime(5574): at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-19 08:49:25.019: E/AndroidRuntime(5574): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.lang.Thread.run(Thread.java:856)
02-19 08:49:25.019: E/AndroidRuntime(5574): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=192.168.133.84/SlimandSave/try.php
02-19 08:49:25.019: E/AndroidRuntime(5574): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
02-19 08:49:25.019: E/AndroidRuntime(5574): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
02-19 08:49:25.019: E/AndroidRuntime(5574): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
02-19 08:49:25.019: E/AndroidRuntime(5574): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
02-19 08:49:25.019: E/AndroidRuntime(5574): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-19 08:49:25.019: E/AndroidRuntime(5574): at com.college.slimandsave.FoodSearchActivity$MyAsyncTask.postData(FoodSearchActivity.java:106)
02-19 08:49:25.019: E/AndroidRuntime(5574): at com.college.slimandsave.FoodSearchActivity$MyAsyncTask.doInBackground(FoodSearchActivity.java:81)
02-19 08:49:25.019: E/AndroidRuntime(5574): at com.college.slimandsave.FoodSearchActivity$MyAsyncTask.doInBackground(FoodSearchActivity.java:1)
02-19 08:49:25.019: E/AndroidRuntime(5574): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-19 08:49:25.019: E/AndroidRuntime(5574): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-19 08:49:25.019: E/AndroidRuntime(5574): ... 5 more

编辑.....以上是我的最新代码和错误日志。在php端,我只有fileputcontents可以写入文本文件。

编辑...在IP地址似乎可以正常工作之前添加http://,而不是在txtFoodSearch.getText()。toString()的namevalue对中使用fSearch字符串。

最佳答案

SearchActivity不是 Activity 类

你有

Intent intSearch = new Intent(FoodSearchActivity.this, SearchActivity.class);
startActivity(intSearch);

它是一个AsyncTask类
public static class SearchActivity extends AsyncTask<String, Void, String> {

调用Asynctask使用
new SearchActivity().execute(params);

您的 doInbackground不执行任何操作。

您在 postData(View v)中有网络操作,应该在 doInbackground中进行

你也有
 <activity android:name="com.college.slimandsave.FoodSearchActivity$SearchActivity" >    
</activity>

应该将其删除。

关于android - Android错误日志, list 中的 Activity ,FATAL EXCEPTION : AsyncTask #1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21854703/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com