当我在android 4上运行它时,我的应用程序崩溃了

时间:2016-04-12 09:13:47

标签: java android

我想要你的帮助,当我在5或6号机器人上运行时,我的应用程序正常工作, 但是当我在android 4上运行它时会崩溃。

Maincativit.java

package com.onecode.humam.me;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

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


public class MainActivity extends AppCompatActivity {
    list1 adapter;

    List<items> n=new ArrayList<>();

    private RecyclerView rv;
public items news_object;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.rv = (RecyclerView) findViewById(R.id.rv);
        adapter=new list1(this,n);
        rv.setHasFixedSize(true);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setAdapter(adapter);



        new AsyncTask<String, Integer, Document>() {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                Toast.makeText(MainActivity.this, "Start Loading Data", Toast.LENGTH_SHORT).show();
            }

            @Override
            protected Document doInBackground(String... params) {
                Document doc = null;
                try {
                    doc = Jsoup.connect("http://www.non14.net/ملف-الأخبار/").get();


                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(MainActivity.this, " not work", Toast.LENGTH_SHORT).show();
                }

                return doc;

            }

            @Override
            protected void onPostExecute(Document document) {
                 super.onPostExecute(document);
                Toast.makeText(MainActivity.this, "finish Loading Data", Toast.LENGTH_SHORT).show();
                Elements news = document.select("div.contentcolumn");
                Elements title = news.select("div.catitems");
                Log.d("MainActivity", "This is news = " + title);

                for (Element el : title) {
                      news_object = new items();
                    news_object.setTitle(el.select("a").text());
                    news_object.setauther(el.select("a").attr("abs:href"));
                    //  news_object.setDate(el.select("time").text());
                   // news_object.setDesc(el.select("div.cb-excerpt").select("p").text());
                 news_object.setimg(el.select("img").attr("abs:src"));
                  //  news_object.setNp_views(el.select("div.cb-post-views.cb-byline-element").text());
                    //news_object.setAuther(el.select("div.cb-author.cb-byline-element").text());
                    Log.d("MainActivity", "This is news = " + news_object.getauther());


                    //Intent i=new Intent(getApplicationContext(),list1.class);

                    //ADD DATA TO OUR INTENT
                    //i.putExtra("Name", news_object.gettitle());


                    n.add(news_object);
                    adapter.notifyDataSetChanged();



//                    text.append("\n " + " " + el.select("h2").text());
//                    text.append("\n  " + el.select("img").attr("src"));
//                    text.append("\n " + el.select("time").text());
//                    text.append("\n " + el.select("div.cb-author.cb-byline-element").text());
//                    text.append("\n " + el.select("div.cb-post-views.cb-byline-element").text());
//                    text.append("\n " + el.select("div.cb-excerpt").select("p").text());
                }

            }
        }.execute();


    }
}

mainactivit.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.tatbigy.jsoupandroidtut.MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#eee"
        android:padding="10dp"
        app:popupTheme="@style/TextAppearance.AppCompat">


        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher" />

    </android.support.v7.widget.Toolbar>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

和logcat

unable to find class referenced in signature (Landroid/view/SearchEvent;)
unable to resolve interface method 18299: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
VFY: unable to resolve interface method 18303: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
VFY: unable to resolve virtual method 451: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
VFY: unable to resolve virtual method 473: Landroid/content/res/TypedArray;.getType (I)I
java.net.UnknownHostException: Unable to resolve host "www.non14.net": No address associated with hostname
    at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
    at java.net.InetAddress.getAllByName(InetAddress.java:214)
    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:362)
    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
    at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:512)
    at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:493)
    at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:205)
    at org.jsoup.helper.HttpConnection.get(HttpConnection.java:194)
    at com.onecode.humam.me.MainActivity$1.doInBackground(MainActivity.java:52)
    at com.onecode.humam.me.MainActivity$1.doInBackground(MainActivity.java:41)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
    at libcore.io.Posix.getaddrinfo(Native Method)
    at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:59)
    at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
    ... 24 more
Caused by: libcore.io.ErrnoException: getaddrinfo failed: ENETUNREACH (Network is unreachable)
    ... 27 more
threadid=11: thread exiting with uncaught exception (group=0x41b18960)
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:299)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
    at java.util.concurrent.FutureTask.run(FutureTask.java:239)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:856)
 Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    at android.os.Handler.<init>(Handler.java:197)
    at android.os.Handler.<init>(Handler.java:111)
    at android.widget.Toast$TN.<init>(Toast.java:370)
    at android.widget.Toast.<init>(Toast.java:104)
    at android.widget.Toast.makeText(Toast.java:261)
    at com.onecode.humam.me.MainActivity$1.doInBackground(MainActivity.java:57)
    at com.onecode.humam.me.MainActivity$1.doInBackground(MainActivity.java:41)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
    at java.lang.Thread.run(Thread.java:856) 

4 个答案:

答案 0 :(得分:0)

您的应用程序崩溃了,这与Android版本无关,但是UnknownHostException您尝试访问主机“ www.non14.net ”但可能是服务器不存在......

答案 1 :(得分:0)

错误信息非常明确:Unable to resolve host "www.non14.net": No address associated with hostname,可能是由此行引起的

doc = Jsoup.connect("http://www.non14.net/ملف-الأخبار/").get();

答案 2 :(得分:0)

尝试将此添加到您的清单

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

答案 3 :(得分:0)

你必须改变这个:

} catch (IOException e)
    e.printStackTrace();
    Toast.makeText(MainActivity.this, " not work", Toast.LENGTH_SHORT).show();
}

将其更改为

} catch (IOException e) {
    e.printStackTrace();
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(MainActivity.this, " not work", Toast.LENGTH_SHORT).show();
        }
    });
}