很难找到logcat中显示的错误类型

时间:2014-01-31 05:18:16

标签: android

01-30 23:59:02.322: E/AndroidRuntime(1105): FATAL EXCEPTION: main
01-30 23:59:02.322: E/AndroidRuntime(1105): Process: com.example.radio, PID: 1105
01-30 23:59:02.322: E/AndroidRuntime(1105): android.os.NetworkOnMainThreadException
01-30 23:59:02.322: E/AndroidRuntime(1105):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at com.example.radio.JSONParser.makeHttpRequest(JSONParser.java:38)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at com.example.radio.Answers$GetAnswers$1.run(Answers.java:98)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at android.os.Handler.handleCallback(Handler.java:733)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at android.os.Looper.loop(Looper.java:136)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at android.app.ActivityThread.main(ActivityThread.java:5017)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at java.lang.reflect.Method.invokeNative(Native Method)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at java.lang.reflect.Method.invoke(Method.java:515)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at dalvik.system.NativeStart.main(Native Method)

当我运行我的Android应用程序项目时,这些是在LogCat中显示的各种类型的错误。请帮助我找出那些类型的错误。

这是我的MainActivity编码.....

package com.example.radio;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.app.ActionBar;
public class MainActivity extends Activity 
{
private ProgressDialog pDialog;
JSONParser jParser=new JSONParser();
RadioGroup R1,R2;
RadioButton B1,B2;
Button submit;
    CheckBox C1,C2,C3;
EditText email;
 String s1="";
 private static final String TAG_SUCCESS = "success";
 private static String url_http="http://saravananoct14.web44.net/add.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
R1=(RadioGroup)findViewById(R.id.group1);
R2=(RadioGroup)findViewById(R.id.group2);
C1=(CheckBox)findViewById(R.id.checkBox1);
C2=(CheckBox)findViewById(R.id.checkBox2);
C3=(CheckBox)findViewById(R.id.checkBox3);
email=(EditText)findViewById(R.id.email);

submit=(Button)findViewById(R.id.back);
submit.setOnClickListener(new OnClickListener()
{

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    String simple="";
B1=(RadioButton)findViewById(R1.getCheckedRadioButtonId());
    B2=(RadioButton)findViewById(R2.getCheckedRadioButtonId());     

if(C1.isChecked())
{
    s1=s1.concat(C1.getText().toString());

}   
    if(C2.isChecked())
{
    s1=s1.concat(",");
    s1=s1.concat(C2.getText().toString());

}
if(C3.isChecked())
{
    s1=s1.concat(",");
    s1=s1.concat(C3.getText().toString());

}   
                                                                                         simple=B1.getText().toString()+","+B2.getText().toString()+","+s1+","+email.getText().toString();
Toast.makeText(MainActivity.this, simple, Toast.LENGTH_LONG).show();
new addanswers().execute();
}


});
}
class addanswers extends AsyncTask<String,String,String>
{
    protected void onPreExecute()
{
         super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Adding answers to the database");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

 }

    protected String doInBackground(String... args) 
 {
        // TODO Auto-generated method stub
        String EMAIL_ID=email.getText().toString();
        String Q1=B1.getText().toString();
        String Q2=B2.getText().toString();
        String Q3=s1;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("EMAIL_ID",EMAIL_ID));
params.add(new BasicNameValuePair("Q1",Q1));
params.add(new BasicNameValuePair("Q2",Q2));
params.add(new BasicNameValuePair("Q3",Q3));

JSONObject json=jParser.makeHttpRequest(url_http,"POST",params);        
Log.d("Create Response",json.toString());
try {
    int success = json.getInt(TAG_SUCCESS);

    if (success == 1) {
Intent intent=new Intent(getApplicationContext(),Res.class);
startActivity(intent);

        finish();

    } else {
        }
} catch (JSONException e) {
    e.printStackTrace();
}


return null;
    }
     protected void onPostExecute(String file_url) {
    try{        pDialog.cancel();
    }catch (Exception e){
        e.printStackTrace();

    }
     }

    }

public void onDestroy(){
    super.onDestroy();
    if(pDialog!=null && pDialog.isShowing())
    {
        pDialog.cancel();
    }       

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
public boolean onOptionsItemSelected(MenuItem item) {

    Intent intent=new Intent(getApplicationContext(),Admin.class);
     startActivity(intent);

    return true;
}


  }

那么我应该改变什么?任何核心?请建议我......

2 个答案:

答案 0 :(得分:0)

不应在主线程上运行基于网络的操作。

尝试使用线程

new Thread(new Runnable(){
    @Override
    public void run() {
        try {
            //your code
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}).start();

或使用AsyncTask

答案 1 :(得分:0)

我认为你的主要错误在于以下几行:

01-30 23:59:02.322: E/AndroidRuntime(1105):     at com.example.radio.JSONParser.makeHttpRequest(JSONParser.java:38)
01-30 23:59:02.322: E/AndroidRuntime(1105):     at com.example.radio.Answers$GetAnswers$1.run(Answers.java:98)

查看课程Answers.java (line no : 98)JSONParser.java (line no : 38)

您的主要错误是NetworkOnMainThreadException,当应用程序尝试在其主线程上执行网络操作时,通常会抛出此异常。在AsyncTask

中运行您的代码
class RetreiveFeedTask extends AsyncTask<String, Void, RSSFeed> {

    private Exception exception;

    protected RSSFeed doInBackground(String... urls) {
        try {
            URL url= new URL(urls[0]);
            SAXParserFactory factory =SAXParserFactory.newInstance();
            SAXParser parser=factory.newSAXParser();
            XMLReader xmlreader=parser.getXMLReader();
            RssHandler theRSSHandler=new RssHandler();
            xmlreader.setContentHandler(theRSSHandler);
            InputSource is=new InputSource(url.openStream());
            xmlreader.parse(is);
            return theRSSHandler.getFeed();
        } catch (Exception e) {
            this.exception = e;
            return null;
        }
    }

    protected void onPostExecute(RSSFeed feed) {
        // TODO: check this.exception 
        // TODO: do something with the feed
    }
}

如何执行任务:

 new RetreiveFeedTask().execute(urlToRssFeed);

不要忘记将其添加到AndroidManifest.xml文件:

<uses-permission android:name="android.permission.INTERNET"/>