简体   繁体   English

如何解决android中的异常

[英]how to solve the exception in android

i have written HttpDelete in android to call REST web service. 我已经在android中编写了HttpDelete来调用REST Web服务。

public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView txt = (TextView) findViewById(R.id.textView1);
    txt.setText(getInputStreamFromUrl("http://192.168.37.241:8080/kyaw/k"));
    }

    public static String getInputStreamFromUrl(String url) {
          InputStream content = null;
          HttpResponse response = null;
          try {

            DefaultHttpClient httpclient = new DefaultHttpClient();             
            HttpDelete delete=new HttpDelete(url);
            put.setHeader("Content-Type","application/vnd.org.snia.cdmi.container");
            response = httpclient.execute(delete);
            content = response.getEntity().getContent();
          }catch (Exception e) {
            Log.e("[DELETE REQUEST]", "Network exception");
          }
            String result=response.getStatusLine().toString()+"\n"+response.getHeaders(url);
            return result;
        }

And I get the exception which is 我得到的例外是

05-23 08:30:16.868: ERROR/[DELETE REQUEST](1197): Network exception
05-23 08:30:16.868: DEBUG/AndroidRuntime(1197): Shutting down VM
05-23 08:30:16.878: WARN/dalvikvm(1197): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-23 08:30:16.908: ERROR/AndroidRuntime(1197): FATAL EXCEPTION: main
05-23 08:30:16.908: ERROR/AndroidRuntime(1197): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.putandroid}: java.lang.NullPointerException

Do anyone know why? 有人知道为什么吗?

您是否检查清单文件以获得Internet许可?

No idea for the network exception (yet) but the last NPE is most likely because the previous exception is thrown before response has been initialized through httpclient.execute(delete) . 目前尚不知道网络异常,但最后一个NPE最有可能是因为先前的异常是在通过httpclient.execute(delete)初始化response之前引发的。

You should rewrite your catch block and write the name of the catched exception and the message to the log. 您应该重写catch块,并将catch的异常的名称和消息写入日志。

In the onCreate() function on line 4 we find that after initializing your TextView you are trying to set a value. 在第4行的onCreate()函数中,我们发现初始化TextView您尝试设置一个值。

You must use a Handler object because you cannot update an UI objects while in a separate thread. 必须使用Handler对象,因为在单独的线程中无法更新UI对象。

Here is some link for your help 这是一些帮助您的链接

http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html

http://thedevelopersinfo.com/?p=150 http://thedevelopersinfo.com/?p=150

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM