0

タブレット デザイン用にアプリを拡張し、フラグメントを使用します。

メインアクティビティ:

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         FragmentTransaction transaction = getFragmentManager()
         .beginTransaction();
         transaction.add(R.id.fragmentcontainer1, new list());
         transaction.commit();
    }
}

XML ローダーと LazyAdapter を使用したリスト フラグメント:

public class lst extends Fragment {

    String URL = "URL HERE";
    static final String KEY_COUPON = "coupon"; // parent node
    static final String KEY_ID = "id";
    private boolean onCreateRunned = false;
    ArrayList<HashMap<String, String>> list;

    ListView list;
    LazyAdapter adapter;

    @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list,
                container, false); 



        list = new ArrayList<HashMap<String, String>>();

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL);
        Document doc = parser.getDomElement(xml);

        NodeList nl = doc.getElementsByTagName(KEY_COUPON);
        for (int i = 0; i < nl.getLength(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            list.add(map);
        }

        list=(ListView) view.findViewById(R.id.list);

        adapter= new LazyAdapter(getActivity(), list);        
        list.setAdapter(adapter);

        return view;


    }   

}

およびLazyAdapter

    public class LazyAdapter extends BaseAdapter {
    private Activity activity;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 


    public LazyAdapter(Context appContext, ArrayList<HashMap<String, String>> d) {
        activity = (Activity) appContext;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity);
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)


            vi = inflater.inflate(R.layout.list_row, null);

       // do something here

        return vi;
    }
}

エラーは次のとおりです。

01-29 16:42:14.775: E/AndroidRuntime(2057): FATAL EXCEPTION: main
01-29 16:42:14.775: E/AndroidRuntime(2057): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newcouponstablet/com.example.newcouponstablet.MainActivity}: android.os.NetworkOnMainThreadException
01-29 16:42:14.775: E/AndroidRuntime(2057):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at android.os.Looper.loop(Looper.java:137)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at android.app.ActivityThread.main(ActivityThread.java:5039)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at java.lang.reflect.Method.invoke(Method.java:511)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at dalvik.system.NativeStart.main(Native Method)
01-29 16:42:14.775: E/AndroidRuntime(2057): Caused by: android.os.NetworkOnMainThreadException
01-29 16:42:14.775: E/AndroidRuntime(2057):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at libcore.io.IoBridge.connect(IoBridge.java:112)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at java.net.Socket.connect(Socket.java:842)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at com.example.newcouponstablet.XMLParser.getXmlFromUrl(XMLParser.java:45)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at com.example.newcouponstablet.list.onCreateView(list.java:46)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at android.app.Fragment.performCreateView(Fragment.java:1695)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at android.app.BackStackRecord.run(BackStackRecord.java:682)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at android.app.Activity.performStart(Activity.java:5113)
01-29 16:42:14.775: E/AndroidRuntime(2057):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2153)

誰かがそれを理解するのを手伝ってくれることを願っています.

4

2 に答える 2

0

原因:android.os.NetworkOnMainThreadException

=>これは、メインスレッド自体で長時間実行されるタスクを作成/実装しない場合に発生します

これを回避するには、スレッド化(またはAsyncTask)を実装するか、onCreate()メソッド内で長時間実行されるタスクを作成する前に以下の行を含めることができます。

StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy); 
于 2013-01-29T17:20:29.333 に答える
0
public class lst extends Fragment {

    String URL = "URL HERE";
    static final String KEY_COUPON = "coupon"; // parent node
    static final String KEY_ID = "id";
    private boolean onCreateRunned = false;
    ArrayList<HashMap<String, String>> list;

    ListView list;
    LazyAdapter adapter;

    @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list,
                container, false); 



        list = new ArrayList<HashMap<String, String>>();
        new Thread(new Runnable(){
        public void run() {

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL);
        Document doc = parser.getDomElement(xml);

        NodeList nl = doc.getElementsByTagName(KEY_COUPON);
        for (int i = 0; i < nl.getLength(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            list.add(map);
        }

        //Maybe this must activity.runOnUiThread(...     
        list=(ListView) view.findViewById(R.id.list);

        adapter= new LazyAdapter(getActivity(), list);        
        list.setAdapter(adapter);

        }}).start()


        return view;


    }   

}
于 2013-01-29T18:36:59.943 に答える