[英]Trying to create a custom layout with XML parsing. Error
我一直在遵循有關創建自定義布局和XML解析的教程。 不幸的是,我以自己的方式對其進行了重組。 我在實例上單擊該按鈕,將其引導到使用XML查看自定義布局的活動,該應用程序崩潰了。 可能是網絡錯誤? 還是解析錯誤? 或打開活動錯誤。 無論哪種方式,它都會崩潰。 Logcat似乎同時發布了所有這些問題。
Logcat:
11-29 15:14:07.347:E / AndroidRuntime(1133):致命異常:主11-29 15:14:07.347:E / AndroidRuntime(1133):進程:com.example.clinicbooker,PID:1133 11-29 15:14:07.347:E / AndroidRuntime(1133):java.lang.RuntimeException:無法啟動活動ComponentInfo {com.example.clinicbooker / com.example.clinicbooker.BookScreen}:android.os.NetworkOnMainThreadException 11-29 15: 14:07.347:E / AndroidRuntime(1133):位於android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)11-29 15:14:07.347:E / AndroidRuntime(1133):位於android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2226)11-29 15:14:07.347:E / AndroidRuntime(1133):在android.app.ActivityThread.access $ 700(ActivityThread.java:135)11-29 15:14:07.347:E / AndroidRuntime(1133):位於android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1397)11-29 15:14:07.347:E / AndroidRuntime(1133):位於android.os.Handler.dispatchMessage(Handler.java :102)11-29 15:14:07.347:E / AndroidRuntime(1133):位於android.os.Loop er.loop(Looper.java:137)11-29 15:14:07.347:E / AndroidRuntime(1133):位於android.app.ActivityThread.main(ActivityThread.java:4998)11-29 15:14:07.347: E / AndroidRuntime(1133):at java.lang.reflect.Method.invokeNative(本機方法)11-29 15:14:07.347:E / AndroidRuntime(1133):at java.lang.reflect.Method.invoke(Method。 java:515)11-29 15:14:07.347:E / AndroidRuntime(1133):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:777)11-29 15:14:07.347: E / AndroidRuntime(1133):位於com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)11-29 15:14:07.347:E / AndroidRuntime(1133):位於dalvik.system.NativeStart.main (本地方法)11-29 15:14:07.347:E / AndroidRuntime(1133):原因:android.os.NetworkOnMainThreadException 11-29 15:14:07.347:E / AndroidRuntime(1133):在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)11-29 15:14:07.347:E / AndroidRuntime(1133):at java.net.InetAddress.lookupHostByName(InetAddress.java:385)11-29 15:14:07.347 : E / AndroidRuntime(1133):位於java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)11-29 15:14:07.347:E / AndroidRuntime(1133):位於java.net.InetAddress.getAllByName(InetAddress.java :214)11-29 15:14:07.347:E / AndroidRuntime(1133):位於org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)11-29 15:14:07.347:E / AndroidRuntime(1133):位於org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)11-29 15:14:07.347:E / AndroidRuntime(1133):位於org.apache.http。 impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)11-29 15:14:07.347:E / AndroidRuntime(1133):位於org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360 )11-29 15:14:07.347:E / AndroidRuntime(1133):位於org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)11-29 15:14:07.347:E / AndroidRuntime (1133):位於org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHtt pClient.java:487)11-29 15:14:07.347:E / AndroidRuntime(1133):在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)11-29 15:14: 07.347:E / AndroidRuntime(1133):位於com.example.functionalities.XMLParser.getXmlFromUrl(XMLParser.java:45)11-29 15:14:07.347:E / AndroidRuntime(1133):位於com.example.clinicbooker.BookScreen .onCreate(BookScreen.java:43)11-29 15:14:07.347:E / AndroidRuntime(1133):在android.app.Activity.performCreate(Activity.java:5243)11-29 15:14:07.347:E / AndroidRuntime(1133):位於android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)11-29 15:14:07.347:E / AndroidRuntime(1133):位於android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2140)11-29 15:14:07.347:E / AndroidRuntime(1133):...還有11個
我有我的屏幕,當用戶點擊導致它的片段上的按鈕時,它就會啟動:
public class BookScreen extends Activity {
// All static variables
static final String URL = "https://dl.dropboxusercontent.com/u/42241589/test.xml";
// XML node keys
static final String KEY_CLINIC = "clinic"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_DATE = "date";
static final String KEY_TIME = "time";
static final String KEY_ADDRESS = "address";
static final String KEY_THUMB_URL = "thumb_url";
ListView list;
LazyAdapter adapter;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_main);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_CLINIC);
// looping through all song nodes <song>
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(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
map.put(KEY_TIME, parser.getValue(e, KEY_TIME));
map.put(KEY_ADDRESS, parser.getValue(e, KEY_ADDRESS));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
list = (ListView) findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapter = new LazyAdapter(this, songsList);
list.setAdapter(adapter);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll()
.build();
StrictMode.setThreadPolicy(policy);
// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
});
}
}
其適配器:
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
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.book_list_row, null);
TextView title = (TextView)vi.findViewById(R.id.menu_name);
TextView description = (TextView)vi.findViewById(R.id.address);
TextView bookingDate = (TextView)vi.findViewById(R.id.book_date);
TextView bookingTime = (TextView)vi.findViewById(R.id.book_time);
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image);
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(BookScreen.KEY_TITLE));
description.setText(song.get(BookScreen.KEY_ADDRESS));
bookingDate.setText(song.get(BookScreen.KEY_DATE));
bookingTime.setText(song.get(BookScreen.KEY_TIME));
imageLoader.DisplayImage(song.get(BookScreen.KEY_THUMB_URL), thumb_image);
return vi;
}
}
String xml = parser.getXmlFromUrl(URL);
將其放在AsyncTask中。 不要在UI線程中直接使用它。 這是您的異常NetworkOnMainThreadException
asynctask有很多示例,請使用其中一個。
造成原因:android.os.NetworkOnMainThreadException 11-29
您不能從UI線程訪問數據庫。 試試AsynchTask。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.