我正在尝试使用AsynTask从Internet获取Country微调器的数据。我得到了NoClassDefFoundError,我查了一下,common-io-2.5.jar在classpath中。我正在使用Eclipse。声明Type标记时代码崩溃。 我不知道如何解决这个问题,请帮帮我。非常感谢你!
package fantasticmassage.com;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.renderscript.Type;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import org.apache.commons.io.IOUtils;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
public class CountrySpinner extends Activity{
private Spinner spnCountry;
private ArrayList<Country> countries;
private ArrayAdapter<Country> spnCountryAdapter;
public CountrySpinner(){
}
public CountrySpinner(Spinner spn){
this.spnCountry = spn;
}
public CountrySpinner(Spinner spn, ArrayAdapter<Country> adapter){
this.spnCountry = spn;
this.spnCountryAdapter = adapter;
}
public void getDataForCountrySpinner(){
new CountrySpinnerAsync().execute();
}
public class CountrySpinnerAsync extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute(){
super.onPreExecute();
System.out.println("onPreExecute: passed");
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
URL url = new URL("http://leansigmaway.com/api/ca_api/api.php?type=getCountries");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.connect();
String result = IOUtils.toString(httpURLConnection.getInputStream(), "UTF-8");
JSONObject jsonObject = new JSONObject(result);
String countryList = jsonObject.getJSONArray("result").toString();
Type token = (Type) new TypeToken<ArrayList<Country>>(){}.getType();//Look like it crashes right here
System.out.println("doInBacground, token: " + token);
Gson gson = new Gson();
countries = gson.fromJson(countryList, (java.lang.reflect.Type) token);
spnCountryAdapter = new ArrayAdapter<Country>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, countries);
spnCountryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid){
super.onPostExecute(aVoid);
spnCountry.setAdapter(spnCountryAdapter);
}
}
private class Country implements Comparator<Country> {
@SerializedName("id")
private int countryID;
@SerializedName("name")
private String countryName;
public Country() {
}
public Country(int countryID, String countryName) {
this.countryID = countryID;
this.countryName = countryName;
}
public int getCountryID() {
return countryID;
}
public void setCountryID(int countryID) {
this.countryID = countryID;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
@Override
public String toString() {
return countryName;
}
@Override
public int compare(Country lhs, Country rhs) {
return lhs.getCountryName().compareTo(rhs.getCountryName());
}
}
}
日志文件:
07-21 22:00:59.378: E/AndroidRuntime(1948): FATAL EXCEPTION: AsyncTask #1
07-21 22:00:59.378: E/AndroidRuntime(1948): Process: fantasticmassage.com, PID: 1948
07-21 22:00:59.378: E/AndroidRuntime(1948): java.lang.RuntimeException: An error occured while executing doInBackground()
07-21 22:00:59.378: E/AndroidRuntime(1948): at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-21 22:00:59.378: E/AndroidRuntime(1948): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-21 22:00:59.378: E/AndroidRuntime(1948): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-21 22:00:59.378: E/AndroidRuntime(1948): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-21 22:00:59.378: E/AndroidRuntime(1948): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-21 22:00:59.378: E/AndroidRuntime(1948): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-21 22:00:59.378: E/AndroidRuntime(1948): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-21 22:00:59.378: E/AndroidRuntime(1948): at java.lang.Thread.run(Thread.java:818)
07-21 22:00:59.378: E/AndroidRuntime(1948): Caused by: java.lang.NoClassDefFoundError: fantasticmassage.com.CountrySpinner$CountrySpinnerAsync$1
07-21 22:00:59.378: E/AndroidRuntime(1948): at fantasticmassage.com.CountrySpinner$CountrySpinnerAsync.doInBackground(CountrySpinner.java:74)
07-21 22:00:59.378: E/AndroidRuntime(1948): at fantasticmassage.com.CountrySpinner$CountrySpinnerAsync.doInBackground(CountrySpinner.java:1)
07-21 22:00:59.378: E/AndroidRuntime(1948): at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-21 22:00:59.378: E/AndroidRuntime(1948): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-21 22:00:59.378: E/AndroidRuntime(1948): ... 4 more
答案 0 :(得分:0)
从官方文档中,由于
而引发此异常如果Java虚拟机或ClassLoader实例尝试加载类的定义(作为普通方法调用的一部分或作为使用新表达式创建新实例的一部分),则抛出该类,并且没有类的定义可以被发现。
所以我认为你在第一行代码中为你的类定义提供了错误的路径。尝试在package关键字后给出正确的路径,您的问题将得到解决。希望它有所帮助!
答案 1 :(得分:0)
在NoClassDefFoundError
错误的情况下,我做了以下方法。这对我有用。
转到Project Name Right Click -> Properties -> Java Build Path -> Order and Export -> common-io-2.5.jar (Check this jar)
之后Click common-io-2.5.jar -> Click Up (in right side panel) -> Ok
这可以解决您的问题。
答案 2 :(得分:-3)
newTexture = SDL_CreateTextureFromSurface(*renderer, loaded);
和Clean your project
..像rebuild
这样的大多数问题都会解决这个问题。