我扩展了我的平板电脑设计应用程序并使用碎片。
MainActivity:
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 Loader和LazyAdapter的List Fragment:
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)
我希望任何人都可以帮助我理解它。
答案 0 :(得分:0)
引起:android.os.NetworkOnMainThreadException
=&GT;无论何时在主线程本身上创建/实现长时间运行的任务,都会发生这种情况
要避免这种情况,您可以在onCreate()方法中执行长时间运行的任务之前实现线程(或 AsyncTask )或包含以下行:
StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
答案 1 :(得分: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;
}
}