0

xml ファイルを解析しようとしています。私はたくさん検索し、最終的にこれに関するいくつかのチュートリアルを見つけました。私は自分のコードを書いていますが、今問題があります。関数を開始すると (ラジオボタンを変更して: エラーが表示されます。間違いを修正するのを手伝ってもらえますか?

これが私のコードです:

public class Download_database extends Activity {
    static String URL = "http://ganjoor.sourceforge.net/newgdbs.xml";
    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
      XMLParser parser = new XMLParser();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_download_database);
        RadioGroup download_section_group=(RadioGroup)findViewById(R.id.download_section_grop);
        RadioButton newgdb=(RadioButton)findViewById(R.id.radio_newgdb);
        RadioButton sitegdb=(RadioButton)findViewById(R.id.radio_sitegdb);
        RadioButton programgdb=(RadioButton)findViewById(R.id.radio_programgdb);
        download_section_group.check(R.id.radio_newgdb);
        final TextView hint=(TextView)findViewById(R.id.txt_download_db_hint);
        TabHost tabs=(TabHost)findViewById(R.id.download_cat_tabhost);
        tabs.setup();
        TabHost.TabSpec spec=tabs.newTabSpec("down_tag1");
        spec.setContent(R.id.download_cat_01);
        spec.setIndicator(getString(R.string.txt_download_tab_download_sections));
        tabs.addTab(spec);
        spec=tabs.newTabSpec("down_tag2");
        spec.setContent(R.id.download_cat_02);
        spec.setIndicator(getString(R.string.txt_download_tab_download_list));
        tabs.addTab(spec);



        download_section_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup arg0, int arg1) {

                if(R.id.radio_newgdb== arg1){
                    hint.setText(getString(R.string.txt_download_hint_newgdb));
                    URL = "http://ganjoor.sourceforge.net/newgdbs.xml";
                    xmlp(URL);
                }

                if(R.id.radio_sitegdb== arg1){
                    hint.setText(getString(R.string.txt_download_hint_sitedb));
                    URL = "http://ganjoor.sourceforge.net/sitegdbs.xml";
                    xmlp(URL);

                }

                if(R.id.radio_programgdb== arg1){
                    hint.setText(getString(R.string.txt_download_hint_programgdb));
                    URL = "http://ganjoor.sourceforge.net/programgdbs.xml";
                    xmlp(URL);
                }

            }
        });





        if(isOnline()){


        }
        else{
            Toast.makeText(this, "you are not connected to internet..Please check your connections", 1).show();
        }
    }


    private void xmlp(String url){
        Log.i(URL, "call xmlp");
        String xml = parser.getXmlFromUrl(url); // getting XML

        Log.i("getting DOM element", "getting DOM element");
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(parser.KEY_gdb);
        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(parser.KEY_CatName, parser.getValue(e, parser.KEY_CatName));
            map.put(parser.KEY_PoetID, parser.getValue(e, parser.KEY_PoetID));
            map.put(parser.KEY_DownloadUrl, parser.getValue(e, parser.KEY_DownloadUrl));
            map.put(parser.KEY_PubDate, parser.getValue(e, parser.KEY_PubDate));
            map.put(parser.KEY_FileSizeInByte,"Size:"+ parser.getValue(e,parser.KEY_FileSizeInByte));
            // adding HashList to ArrayList
            menuItems.add(map);
            ListView list=(ListView)findViewById(R.id.database_list);
            ListAdapter adapter = new SimpleAdapter(this, menuItems,R.layout.list_item,new String[] {parser.KEY_CatName,parser.KEY_PoetID, parser.KEY_DownloadUrl ,parser.KEY_PubDate,parser.KEY_FileSizeInByte}, new int[] {
                            R.id.chk_database_list_item, R.id.txt_database_poet_id, R.id.txt_database_download_link,R.id.txt_database_pub_date,R.id.txt_database_size });
            list.setAdapter(adapter);
        }
    }

ここに私のXMLParserクラスがあります:

package co.tosca.persianpoem;

import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.util.Log;

public class XMLParser {
    // XML node keys
    public static final String KEY_gdb = "gdb"; // parent node
    public static final String KEY_CatName = "CatName";
    public static final String KEY_PoetID = "PoetID";
    public static final String KEY_DownloadUrl = "DownloadUrl";
    public static final String KEY_FileSizeInByte = "FileSizeInByte";
    public static final String KEY_PubDate = "PubDate";
    public String getXmlFromUrl(String url) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }

    public Document getDomElement(String xml){
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
                is.setCharacterStream(new StringReader(xml));
                doc = db.parse(is); 

            } catch (ParserConfigurationException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (SAXException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            } catch (IOException e) {
                Log.e("Error: ", e.getMessage());
                return null;
            }
                // return DOM
            return doc;
    }

    public String getValue(Element item, String str) {
        NodeList n = item.getElementsByTagName(str);
        return this.getElementValue(n.item(0));
    }

    public final String getElementValue( Node elem ) {
             Node child;
             if( elem != null){
                 if (elem.hasChildNodes()){
                     for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
                         if( child.getNodeType() == Node.TEXT_NODE  ){
                             return child.getNodeValue();
                         }
                     }
                 }
             }
             return "";
      }

}

最終的に私が応答するエラー:

04-12 21:35:08.822: I/http://ganjoor.sourceforge.net/sitegdbs.xml(2460): call xmlp
04-12 21:35:08.872: D/AndroidRuntime(2460): Shutting down VM
04-12 21:35:08.872: W/dalvikvm(2460): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
04-12 21:35:08.902: E/AndroidRuntime(2460): FATAL EXCEPTION: main
04-12 21:35:08.902: E/AndroidRuntime(2460): android.os.NetworkOnMainThreadException
04-12 21:35:08.902: E/AndroidRuntime(2460):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1108)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:580)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:512)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:490)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at co.tosca.persianpoem.XMLParser.getXmlFromUrl(XMLParser.java:42)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at co.tosca.persianpoem.Download_database.xmlp(Download_database.java:123)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at co.tosca.persianpoem.Download_database.access$0(Download_database.java:121)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at co.tosca.persianpoem.Download_database$1.onCheckedChanged(Download_database.java:94)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at android.widget.RadioGroup.access$600(RadioGroup.java:52)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at android.widget.CompoundButton.setChecked(CompoundButton.java:132)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at android.widget.CompoundButton.toggle(CompoundButton.java:91)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at android.widget.RadioButton.toggle(RadioButton.java:81)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at android.widget.CompoundButton.performClick(CompoundButton.java:103)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at android.view.View$PerformClick.run(View.java:14263)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at android.os.Handler.handleCallback(Handler.java:605)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at android.os.Looper.loop(Looper.java:137)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at android.app.ActivityThread.main(ActivityThread.java:4441)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at java.lang.reflect.Method.invokeNative(Native Method)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at java.lang.reflect.Method.invoke(Method.java:511)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-12 21:35:08.902: E/AndroidRuntime(2460):     at dalvik.system.NativeStart.main(Native Method)

これが私の許可です:

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

これらのxmlファイルの1つを含む文字列をxmlとして使用しようとしました(インターネットへの接続またはxmlの解析に問題があることを確認するため)が、エラーも発生します。

    04-12 21:33:24.032: I/http://ganjoor.sourceforge.net/sitegdbs.xml(1972): call xmlp
    04-12 21:33:24.032: I/getting DOM element(1972): getting DOM element
    04-12 21:33:24.072: D/AndroidRuntime(1972): Shutting down VM
    04-12 21:33:24.072: W/dalvikvm(1972): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
    04-12 21:33:24.082: E/AndroidRuntime(1972): FATAL EXCEPTION: main
    04-12 21:33:24.082: E/AndroidRuntime(1972): java.lang.NullPointerException
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at co.tosca.persianpoem.Download_database.xmlp(Download_database.java:190)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at co.tosca.persianpoem.Download_database.access$0(Download_database.java:121)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at co.tosca.persianpoem.Download_database$1.onCheckedChanged(Download_database.java:94)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at android.widget.RadioGroup.access$600(RadioGroup.java:52)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at android.widget.CompoundButton.setChecked(CompoundButton.java:132)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at android.widget.CompoundButton.toggle(CompoundButton.java:91)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at android.widget.RadioButton.toggle(RadioButton.java:81)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at android.widget.CompoundButton.performClick(CompoundButton.java:103)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at android.view.View$PerformClick.run(View.java:14263)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at android.os.Handler.handleCallback(Handler.java:605)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at android.os.Handler.dispatchMessage(Handler.java:92)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at android.os.Looper.loop(Looper.java:137)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at android.app.ActivityThread.main
(ActivityThread.java:4441)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at java.lang.reflect.Method.invoke(Method.java:511)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    04-12 21:33:24.082: E/AndroidRuntime(1972):     at dalvik.system.NativeStart.main(Native Method)

編集:私を助けてくれたすべての友人に感謝します..いいえ、問題の1つを修正したと思います.downloadxml関数 に追加しましたAsyncTaskが、ネットワークエラーはもう発生しません..ここに私の新しいダウンロードxml関数があります

   public class getxml extends AsyncTask<String, Void,String>{

        @Override
        protected String doInBackground(String... url) {

            try {
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url[0]);

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                xml = EntityUtils.toString(httpEntity);

            } catch (UnsupportedEncodingException e) {
               // e.printStackTrace();
            } catch (ClientProtocolException e) {
               // e.printStackTrace();
            } catch (IOException e) {
               // e.printStackTrace();
            }

            Log.i("getxml", xml);
            return xml;
        }

    }

しかし今、私はNullPointerExceptionそれが起こっている理由を見つけることができません..その理由をどのように見つけなければなりませんか?

編集:nullexpept エラーを修正するために多くのことを試みましたが、その理由が見つかりません。 これが私のコードです:

  private void xmlp(String url){
        Log.i(URL, "call xmlp");
        //String xml = parser.getXmlFromUrl(url); // getting XML old way


        new getxml().execute(url);// getting XML new way
        Log.i("xml Content", xml);
        try{
        Document doc = parser.getDomElement(xml); // getting DOM element
        NodeList nl = doc.getElementsByTagName(parser.KEY_gdb);
        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);

            // adding each child node to HashMap key => value
            map.put(parser.KEY_CatName, parser.getValue(e, parser.KEY_CatName));
            map.put(parser.KEY_PoetID, parser.getValue(e, parser.KEY_PoetID));
            map.put(parser.KEY_DownloadUrl, parser.getValue(e, parser.KEY_DownloadUrl));
            map.put(parser.KEY_PubDate, parser.getValue(e, parser.KEY_PubDate));
            map.put(parser.KEY_FileSizeInByte,"Size:"+ parser.getValue(e,parser.KEY_FileSizeInByte));
            // adding HashList to ArrayList
            menuItems.add(map);

        }
        Log.i("menuItems", menuItems.toString());
        ListView list=(ListView)findViewById(R.id.database_list);
        ListAdapter adapter = new SimpleAdapter(this, menuItems,R.layout.database_list_item,new String[] {parser.KEY_CatName}, new int[] {
                R.id.chk_database_list_item });

      list.setAdapter(adapter);

}
catch (NullPointerException ex){
    Log.i("NullPointerException", ex.getMessage());
  }
    }

このエラーが発生しました

    04-13 12:21:50.372: I/menuItems(3295): [{DownloadUrl=http://sourceforge.net/projects/ganjoor/files/gdb/abvsEid-abvalkhir.zip, PubDate=2011-09-24, FileSizeInByte=Size:123331, CatName=ابوسعيد ابوالخير, PoetID=26}, {DownloadUrl=http://sourceforge.net/projects/ganjoor/files/gdb/asdi-tvsi.zip, PubDate=2012-05-05, FileSizeInByte=Size:559711, CatName=اسدي توسي, PoetID=52}, {DownloadUrl=http://sourceforge.net/projects/ganjoor/files/gdb/aghbal-lahvri.zip, PubDate=2011-09-24, FileSizeInByte=Size:551922, CatName=اقبال لاهوري, PoetID=42}, {DownloadUrl=http://sourceforge.net/projects/ganjoor/files/gdb/amirkhsrv-dhlvi.zip, PubDate=2011-09-24, FileSizeInByte=Size:505209, CatName=اميرخسرو دهلوي, PoetID=34}, {DownloadUrl=http://sourceforge.net/projects/ganjoor/files/gdb/anvri.zip, PubDate=2011-09-24, FileSizeInByte=Size:902760, CatName=انوري, PoetID=18}, {DownloadUrl=http://sourceforge.net/projects/ganjoor/files/gdb/avhdi.zip, PubDate=2011-09-24, FileSizeInByte=Size:991556, CatName=اوحدي, PoetID=19}, {DownloadUrl=http://sourceforge.net/projects/ganjoor/files/gdb/babatahr.zip, PubDate=2011-09-24, FileSizeInByte=Size:60641, CatName=باباطاهر, PoetID=28}, {DownloadUrl=http://sourceforge.net/projects/ganjoor/files/gdb/bidl-dhlvi.zip, PubDate=2011-09-24, FileSizeInByte=Size:2406463, CatName=بيدل دهلوي, PoetID=43}]
04-13 12:13:06.912: D/AndroidRuntime(3149): Shutting down VM
04-13 12:13:06.912: W/dalvikvm(3149): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
04-13 12:13:06.912: E/AndroidRuntime(3149): FATAL EXCEPTION: main
04-13 12:13:06.912: E/AndroidRuntime(3149): java.lang.NullPointerException
04-13 12:13:06.912: E/AndroidRuntime(3149):     at co.tosca.persianpoem.Download_database.xmlp(Download_database.java:258)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at co.tosca.persianpoem.Download_database.access$0(Download_database.java:132)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at co.tosca.persianpoem.Download_database$1.onCheckedChanged(Download_database.java:104)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at android.widget.RadioGroup.access$600(RadioGroup.java:52)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at android.widget.CompoundButton.setChecked(CompoundButton.java:132)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at android.widget.CompoundButton.toggle(CompoundButton.java:91)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at android.widget.RadioButton.toggle(RadioButton.java:81)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at android.widget.CompoundButton.performClick(CompoundButton.java:103)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at android.view.View$PerformClick.run(View.java:14263)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at android.os.Handler.handleCallback(Handler.java:605)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at android.os.Looper.loop(Looper.java:137)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at android.app.ActivityThread.main(ActivityThread.java:4441)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at java.lang.reflect.Method.invoke(Method.java:511)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-13 12:13:06.912: E/AndroidRuntime(3149):     at dalvik.system.NativeStart.main(Native Method)

ご覧のとおり、menuItemsをログに記録しましたが、xmlからの読み取りには問題はありません..問題はリストアダプターにあると思います:(エラーが発生する行は258で list.setAdapter(adapter);、カスタムリストレイアウトを確認しましたが、何も見つからないので、私のカスタムレイアウトをここに投稿しました

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/chk_database_list_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:text="CheckBox" />

        <TextView
            android:id="@+id/txt_database_size"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical|end"
            android:text="TextView" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/txt_database_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/txt_database_pub_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="TextView" />

        <TextView
            android:id="@+id/txt_database_download_link"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <TextView
            android:id="@+id/txt_database_poet_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

</LinearLayout>

興味深いのは、この行を関数に追加したときです

catch (NullPointerException ex){
    Log.i("NullPointerException", ex.getMessage());
  }

この新しいエラーが発生しました

04-13 12:21:50.372: W/dalvikvm(3295): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
04-13 12:21:50.382: E/AndroidRuntime(3295): FATAL EXCEPTION: main
04-13 12:21:50.382: E/AndroidRuntime(3295): java.lang.NullPointerException: println needs a message
04-13 12:21:50.382: E/AndroidRuntime(3295):     at android.util.Log.println_native(Native Method)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at android.util.Log.i(Log.java:159)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at co.tosca.persianpoem.Download_database.xmlp(Download_database.java:263)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at co.tosca.persianpoem.Download_database.access$0(Download_database.java:132)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at co.tosca.persianpoem.Download_database$1.onCheckedChanged(Download_database.java:112)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at android.widget.RadioGroup.access$600(RadioGroup.java:52)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:342)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at android.widget.CompoundButton.setChecked(CompoundButton.java:132)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at android.widget.CompoundButton.toggle(CompoundButton.java:91)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at android.widget.RadioButton.toggle(RadioButton.java:81)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at android.widget.CompoundButton.performClick(CompoundButton.java:103)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at android.view.View$PerformClick.run(View.java:14263)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at android.os.Handler.handleCallback(Handler.java:605)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at android.os.Looper.loop(Looper.java:137)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at android.app.ActivityThread.main(ActivityThread.java:4441)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at java.lang.reflect.Method.invokeNative(Native Method)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at java.lang.reflect.Method.invoke(Method.java:511)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-13 12:21:50.382: E/AndroidRuntime(3295):     at dalvik.system.NativeStart.main(Native Method)

行 263 には以下が含まれます。

Log.i("NullPointerException", ex.getMessage());
4

2 に答える 2

1

メイン UI スレッド内でxmlp(URL);呼び出すことはできません。onCheckedChanged()このインターネット呼び出しは別のスレッドで実行する必要があります。これを行うには多くの方法があります。最も一般的でベスト プラクティスは、AsyncTask.

于 2013-04-12T17:48:42.443 に答える
1

getXmlFromUrl(String url)ネットワーク リクエストの作成によるエラーを処理するには、 AsyncTaskクラスを拡張して別のスレッドを開始します。ロジックをdoInBackground(...)AsyncTask クラスのメソッドに単純に転送し、メソッドを使用してデータを返しますonPostExecute(...)。例は Google で見つけることができます。

于 2013-04-12T17:48:48.393 に答える