我正在开发一个Android应用程序而且我遇到了问题; 我有兼容性错误;我的应用程序与版本2.2和2.3一起正常工作,但在比2.3更新的版本中,只有第一个类正确执行(只包含一个图像和progressBar),之后,我得到一个android运行时错误 那么你们中的任何人都可以帮助我吗?
MainActivty.java:
public class MainActivity extends Activity {
int myProgress=0;
ProgressDialog pg;
ProgressBar b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b=(ProgressBar)findViewById(R.id.progressBar1);
Test t= new Test();
t.execute();}
private class Test extends AsyncTask<Void, Integer, String> {
@Override
protected void onPreExecute(){
super.onPreExecute();
myProgress=0;
}
@Override
protected String doInBackground(Void... arg0) {
String ch = "non connecter";
ConnectivityManager cm =(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
while(myProgress<100){
myProgress++;
b.setProgress(myProgress);
SystemClock.sleep(30);
ch="connecter";
}
}
else{
ch="non connecter";
}
return ch;
}
@Override
protected void onPostExecute(String result) {
if(result.equals("non connecter")){
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setMessage("Problème de connexion !!");
adb.setPositiveButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
adb.show();
}
else{
startActivity(new Intent(MainActivity.this, Choix.class));
finish();
}
}
}
}
Choix.java:
public class Choix extends Activity {
JSONArray ja1 = null;
String ch2="";String ch3="";
TextView t1,t2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choix_espace);
Button b_p=(Button)findViewById(R.id.b_par);
Button b_en=(Button)findViewById(R.id.b_prof);
Button b_el=(Button)findViewById(R.id.b_el);
Button b_ad=(Button)findViewById(R.id.b_admin);
try{
URL url2 = new URL("http://192.168.1.3/web_service/ecole.php");
HttpURLConnection httpconn2 = (HttpURLConnection)url2.openConnection();
if (httpconn2.getResponseCode() == HttpURLConnection.HTTP_OK){
try{ BufferedReader input = new BufferedReader(new InputStreamReader(httpconn2.getInputStream()),8192);
String line1 = null;
while ((line1 = input.readLine()) != null){
ja1 = new JSONArray(line1);}
for (int i = 0; i < ja1.length(); i++) {
JSONObject jo1 = null;
jo1 = (JSONObject) ja1.getJSONObject(i);
ch2 = jo1.getString("nom_ecole");
ch3 = jo1.getString("annee_scolaire");
}
}
finally{
httpconn2.disconnect();
}
}
}catch (JSONException e) {
System.out.print("vérifier !");e.printStackTrace();} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
t1=(TextView)findViewById(R.id.nom);
t1.setText(Farsi.Convert(ch2));
t1.setTextColor(getResources().getColor(R.color.blue));
t2=(TextView)findViewById(R.id.ann);
t2.setText(Farsi.Convert(ch3));
t2.setTextColor(getResources().getColor(R.color.blue));
b_p.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Choix.this,Identi.class);
i.putExtra("id","parent");
Choix.this.startActivity(i);
}
});
b_en.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent i = new Intent(Choix.this,Identi.class);
i.putExtra("id","enseignant");
Choix.this.startActivity(i);
}
});
b_el.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent i = new Intent(Choix.this,Identi.class);
i.putExtra("id","eleve");
Choix.this.startActivity(i);
}
});
b_ad.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent i = new Intent(Choix.this,Identi.class);
i.putExtra("id","admin");
Choix.this.startActivity(i);
}
});}
}
答案 0 :(得分:1)
对于choix.java
类,您需要执行ASYNC
任务中的所有网络操作。你是在主线程上做的。将网络代码放入ASYNC
任务。