下一个代码应该将数据解析为listView
我正在使用的XmlPullParser
方法,但是当我运行此活动时,应用程序崩溃:(我在另一个类中使用了相同的代码并且工作完美,但在这里我不知道原因??logCat
阻止了我在 :rewardsList.setAdapter(adapter);
//Class:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.rewards);
rewards2=new ArrayList<HashMap<String,String>>();
rewardsList=(ListView) findViewById(R.id.reward);
tv=(TextView) findViewById(R.id.text);
try {
url=new URL("http://api.androidhive.info/pizza/?format=xml");
xpf=XmlPullParserFactory.newInstance();
xpf.setNamespaceAware(false);
xpp=xpf.newPullParser();
xpp.setInput(getInputStream(url),"UTF_8");
insideItem = false;
HashMap<String, String> map=new HashMap<String, String>();
int type=xpp.getEventType();
while(type!=XmlPullParser.END_DOCUMENT){
if(type==XmlPullParser.START_TAG){
if(xpp.getName().equalsIgnoreCase("item")){
map=new HashMap<String, String>();
insideItem=true;
}else if(xpp.getName().equalsIgnoreCase("id")){
if(insideItem)
// map.add(xpp.nextText());
map.put("item", xpp.nextText());
}//
}else if(type==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
rewards2.add(map);
}
type=xpp.nextTag();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this,rewards2, R.layout.rewards_list,
new String[] {"item"}, new int[]
{R.id.t3});
rewardsList.setAdapter(adapter);
//tv.setText(rewards2.get(0));
}
private InputStream getInputStream(URL url2) {
// TODO Auto-generated method stub
try{
return url.openConnection().getInputStream();
}catch(IOException e){
return null;
}
日志猫:
10-23 06:43:54.365: E/AndroidRuntime(937): at com.university.app.Rwards.onCreate(Rwards.java:131)
10-23 06:49:28.995: E/AndroidRuntime(1006): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.university.app/com.university.app.Rwards}: java.lang.NullPointerException
10-23 06:49:28.995: E/AndroidRuntime(1006): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10-23 06:49:28.995: E/AndroidRuntime(1006): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10-23 06:49:28.995: E/AndroidRuntime(1006): at android.app.ActivityThread.access$600(ActivityThread.java:123)
10-23 06:49:28.995: E/AndroidRuntime(1006): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
10-23 06:49:28.995: E/AndroidRuntime(1006): at android.os.Handler.dispatchMessage(Handler.java:99)
10-23 06:49:28.995: E/AndroidRuntime(1006): at android.os.Looper.loop(Looper.java:137)
10-23 06:49:28.995: E/AndroidRuntime(1006): at android.app.ActivityThread.main(ActivityThread.java:4424)
10-23 06:49:28.995: E/AndroidRuntime(1006): at java.lang.reflect.Method.invokeNative(Native Method)
10-23 06:49:28.995: E/AndroidRuntime(1006): at java.lang.reflect.Method.invoke(Method.java:511)
10-23 06:49:28.995: E/AndroidRuntime(1006): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-23 06:49:28.995: E/AndroidRuntime(1006): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-23 06:49:28.995: E/AndroidRuntime(1006): at dalvik.system.NativeStart.main(Native Method)
10-23 06:49:28.995: E/AndroidRuntime(1006): Caused by: java.lang.NullPointerException
10-23 06:49:28.995: E/AndroidRuntime(1006): at com.university.app.Rwards.onCreate(Rwards.java:132)
10-23 06:49:28.995: E/AndroidRuntime(1006): at android.app.Activity.performCreate(Activity.java:4465)
10-23 06:49:28.995: E/AndroidRuntime(1006): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-23 06:49:28.995: E/AndroidRuntime(1006): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
xml布局:奖励:
<?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"
android:background="@drawable/bg">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bar">
<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Rwards"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/white_strip_4header" >
<TextView
android:id="@+id/textView1"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_weight="1.10"
android:text="notes" />
<TextView
android:id="@+id/textView2"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_weight="1.10"
android:text="Amount owed" />
<TextView
android:id="@+id/textView3"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_weight="1.10"
android:text="month"
android:gravity="center"/>
<TextView
android:id="@+id/textView4"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_weight="1.10"
android:text="year"
android:gravity="center"/>
</LinearLayout>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ListView
android:id="@+id/reward"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
奖励清单
<?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" >
<TextView
android:id="@+id/t3"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1"
android:layout_margin="1dp"/>
<TextView
android:id="@+id/t2"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="2dp"
android:gravity="left">
</TextView>
<TextView
android:id="@+id/t1"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1"
android:layout_margin="1dp"
/>
</LinearLayout>
我试图在没有使用textView
它的列表的情况下运行它也崩溃了!!所以请有人知道为什么?我已经将活动添加到了manifest
!