我正在尝试从网络服务器获取文本,来自json的图像并放入网格视图 从服务器获取数据时没关系,但问题在于获取视图
这里只是试图获取文本,但它会在logcat中弹出一个关于getview方法的错误列表
Logcat错误:
02-15 20:51:13.628: E/AndroidRuntime(1135): FATAL EXCEPTION: main
02-15 20:51:13.628: E/AndroidRuntime(1135): java.lang.NullPointerException
02-15 20:51:13.628: E/AndroidRuntime(1135): at de.vogella.android.twitter.json.ParseJSON$ImageAdapter.getView(ParseJSON.java:211)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.widget.GridView.onMeasure(GridView.java:932)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.view.View.measure(View.java:8171)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.view.View.measure(View.java:8171)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.view.View.measure(View.java:8171)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.view.View.measure(View.java:8171)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.view.View.measure(View.java:8171)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.os.Looper.loop(Looper.java:123)
02-15 20:51:13.628: E/AndroidRuntime(1135): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-15 20:51:13.628: E/AndroidRuntime(1135): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 20:51:13.628: E/AndroidRuntime(1135): at java.lang.reflect.Method.invoke(Method.java:521)
02-15 20:51:13.628: E/AndroidRuntime(1135): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-15 20:51:13.628: E/AndroidRuntime(1135): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-15 20:51:13.628: E/AndroidRuntime(1135): at dalvik.system.NativeStart.main(Native Method)
代码:
public class ParseJSON extends Activity {
GridView MyGrid;
Bitmap bmImg;
ImageView imView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyGrid = (GridView)findViewById(R.id.grid);
MyGrid.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public String readTwitterFeed() {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(
"http://www.zigwheels.com/api/zigtvApi.php?method=data&module=News§ion=News");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(ParseJSON.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
public int getCount() {
return 10;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View MyView = convertView;
if (convertView == null) { // if it's not recycled, initialize some attributes
LayoutInflater li = getLayoutInflater();
MyView = li.inflate(R.layout.main, null);
}
TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);
try{
JSONArray jsonArray = new JSONArray(readTwitterFeed());
String[] values = new String[jsonArray.length()] ;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
jsonObject = jsonArray.getJSONObject(i);
if( position < values.length){
values[i] =jsonObject.getString("headline");
tv.setText(""+ values[position] );
}
}}catch(JSONException e){
e.printStackTrace();
}
return MyView;
}
}
}
答案 0 :(得分:0)
如果您的position
变量大于当前i
,那么您将获得NullPointerException,因为您没有值[position]中的某些内容(这是因为您重置了values数组每次调用getView())。
尝试类似
的内容for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
jsonObject = jsonArray.getJSONObject(i);
values[i] =jsonObject.getString("headline");
if( position < values.length)
tv.setText(""+ values[position] );
// thumbnail[i] =jsonObject.getString("thumbnail");
}