将图片网址解析为图片并在我的listview
中显示时出错。
我已经发现错误但我无法纠正它....(抓住=知道错误在哪里-_-)
首先我使用SAX。 其次,我正在进行各种解析,所以我在一个活动中粉碎了所有东西。 第三,我将发送日志而不会发现异常。
错误是最后一段代码......其中有try和catch。
public class MainActivity extends Activity {
URL url;
ListView list;
ArrayList<String> titles = new ArrayList<String>();
ArrayList<String> artists = new ArrayList<String>();
ArrayList<String> countries = new ArrayList<String>();
ArrayList<String> companies = new ArrayList<String>();
ArrayList<String> prices = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView1);
MyAsync ma = new MyAsync();
ma.execute();
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
if(position == 0){
Intent a = new Intent(MainActivity.this , custom.class);
tartActivity(a);
}
}
});
}
class MyAsync extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = ProgressDialog.show(MainActivity.this, "", "loading pc parts");
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
url = new URL("https://dl.dropboxusercontent.com/u/74662678/graphicscards.xml");
// Create an instance for SAXParserFactory
SAXParserFactory mySAXParserFactory = SAXParserFactory
.newInstance();
// Create an instance for SAXParser
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
// Create an instance for XMLReader
XMLReader myXMLReader = mySAXParser.getXMLReader();
// Create an instance for customized handler class
XMLHandler myXMLHandler = new XMLHandler();
// apply handler to the XMLReader
myXMLReader.setContentHandler(myXMLHandler);
// open the connection
InputSource is = new InputSource(url.openStream());
// parse the data
myXMLReader.parse(is);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.cancel();
CustomAdapter ca = new CustomAdapter();
list.setAdapter(ca);
}
}
class XMLHandler extends DefaultHandler {
boolean TITLE = false;
boolean BRAND = false;
boolean USE = false;
boolean URL = false;
boolean COST = false;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
if (localName.equals("TITLE")) {
this.TITLE = true;
} else if (localName.equals("BRAND")) {
this.BRAND = true;
} else if (localName.equals("USE")) {
this.USE = true;
} else if (localName.equals("URL")) {
this.URL = true;
} else if (localName.equals("COST")) {
this.COST = true;
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
if (this.TITLE) {
titles.add(new String(ch, start, length));
} else if (this.BRAND) {
artists.add(new String(ch, start, length));
} else if (this.USE) {
countries.add(new String(ch, start, length));
} else if (this.URL) {
companies.add(new String(ch, start, length));
} else if (this.COST) {
prices.add(new String(ch, start, length));
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
if (localName.equals("TITLE")) {
this.TITLE = false;
} else if (localName.equals("BRAND")) {
this.BRAND = false;
} else if (localName.equals("USE")) {
this.USE = false;
} else if (localName.equals("URL")) {
this.URL = false;
} else if (localName.equals("COST")) {
this.COST = false;
}
}
}
class CustomAdapter extends BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return titles.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int index, View v, ViewGroup arg2) {
// TODO Auto-generated method stub
v = getLayoutInflater().inflate(R.layout.custom, null);
TextView t1 = (TextView) v.findViewById(R.id.title);
TextView t2 = (TextView) v.findViewById(R.id.artist);
TextView t3 = (TextView) v.findViewById(R.id.textView1);
TextView t4 = (TextView) v.findViewById(R.id.duration);
String song = new String();
song = titles.get(index);
t1.setText(titles.get(index).toString());
t2.setText(artists.get(index).toString());
t3.setText(countries.get(index).toString());
t4.setText(prices.get(index).toString());
//loading list picture
String a = companies.get(index).toString();
ImageView lblThumb = (ImageView) findViewById(R.id.thumbie);
try{
int loader = R.drawable.loader;
ImageLoader imgLoader = new ImageLoader(getApplicationContext());
imgLoader.DisplayImage(a, loader, lblThumb);
}catch(Exception e){
Log.d("error" , "lol");
}
return v;
}
}
}
LOG: -
03-19 08:28:34.635: E/AndroidRuntime(3635): FATAL EXCEPTION: main
03-19 08:28:34.635: E/AndroidRuntime(3635): java.lang.NullPointerException
03-19 08:28:34.635: E/AndroidRuntime(3635): at com.ram.saxparserexample.ImageLoader.DisplayImage(ImageLoader.java:48)
03-19 08:28:34.635: E/AndroidRuntime(3635): at com.ram.saxparserexample.MainActivity$CustomAdapter.getView(MainActivity.java:273)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.widget.ListView.makeAndAddView(ListView.java:1831)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.widget.ListView.fillDown(ListView.java:674)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.widget.ListView.fillFromTop(ListView.java:735)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.widget.ListView.layoutChildren(ListView.java:1652)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.View.layout(View.java:14008)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.View.layout(View.java:14008)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.View.layout(View.java:14008)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.View.layout(View.java:14008)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.View.layout(View.java:14008)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.ViewGroup.layout(ViewGroup.java:4373)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.Choreographer.doFrame(Choreographer.java:532)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.os.Handler.handleCallback(Handler.java:725)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.os.Looper.loop(Looper.java:137)
03-19 08:28:34.635: E/AndroidRuntime(3635): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-19 08:28:34.635: E/AndroidRuntime(3635): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 08:28:34.635: E/AndroidRuntime(3635): at java.lang.reflect.Method.invoke(Method.java:511)
03-19 08:28:34.635: E/AndroidRuntime(3635): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-19 08:28:34.635: E/AndroidRuntime(3635): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-19 08:28:34.635: E/AndroidRuntime(3635): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您必须从
更改此设置 ImageView lblThumb = (ImageView) findViewById(R.id.thumbie);
到
ImageView lblThumb = (ImageView) v.findViewById(R.id.thumbie);
因为当你在getView()方法中找到UI控件的ID时,你需要传递View对象的引用来查找ID。
答案 1 :(得分:0)
试试这个..
ImageView lblThumb = (ImageView) v.findViewById(R.id.thumbie);
而不是这个..
ImageView lblThumb = (ImageView) findViewById(R.id.thumbie);
答案 2 :(得分:0)
在ImageLoader
班级中,将您的语境从getApplicationContext()
更改为MainActivity.this
并且还可以在ImageView
方法中按照以下内容访问getView
上下文:
ImageView lblThumb = (ImageView)v.findViewById(R.id.thumbie);