我希望获得一些有关将ListView
更改为GridView
的帮助或建议。自从我在JeansActivity
中重写了代码的某些部分以来,我在修改问题时遇到了问题,因此它可以使用GridView
,但它会向我显示java.lang.NullPointerException
错误:
04-12 19:28:36.194 21114-21114/com.dsgn.bbup E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.dsgn.bbup, PID: 21114
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dsgn.bbup/com.wlodsgn.bunbunup.JeansActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.dsgn.bbup.JeansActivity.onCreate(JeansActivity.java:90)
at android.app.Activity.performCreate(Activity.java:5275)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2166)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
错误可能会显示在第90行gridView.setAdapter(adapter)
以下是OnCreate
方法下的完整代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jeans);
//Back button
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
// enabling action bar app icon and behaving it as toggle button
actionBar.setLogo(R.drawable.ic_main);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
gridView = (GridView) findViewById(R.id.gridView);
adapter = new GridAdapter(this, jeansGrid);
gridView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Cargando Productos...");
pDialog.show();
// changing action bar color
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#1b1b1b")));
// Creating volley request obj
JsonArrayRequest jeansReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Jeans jeans = new Jeans();
jeans.setTitulo(obj.getString("titulo"));
jeans.setThumbnailUrl(obj.getString("image"));
jeans.setThumbnailUrl2(obj.getString("image2"));
jeans.setThumbnailUrl3(obj.getString("image3"));
jeans.setMarca(obj.getString("marca"));
jeans.setColor(obj.getString("color"));
jeans.setTipo(obj.getString("tipo"));
jeans.setRef(obj.getInt("ref"));
// adding jeans to jeans array
jeansGrid.add(jeans);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
/**AppController.getInstance().addToRequestQueue(JeansReq);**/
AppController.getInstance().addToRequestQueue(jeansReq);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String nombre = ((TextView) view.findViewById(R.id.titulo))
.getText().toString();
String brand = ((TextView) view.findViewById(R.id.marca))
.getText().toString();
String color = ((TextView) view.findViewById(R.id.color))
.getText().toString();
String tipo = ((TextView) view.findViewById(R.id.tipo))
.getText().toString();
String ref = ((TextView) view.findViewById(R.id.ref))
.getText().toString();
bitmap = ((Jeans) jeansGrid.get(position)).getThumbnailUrl();
bitmap2 = ((Jeans) jeansGrid.get(position)).getThumbnailUrl2();
bitmap3 = ((Jeans) jeansGrid.get(position)).getThumbnailUrl3();
bitmapfull = ((Jeans) jeansGrid.get(position)).getThumbnailUrlFS();
Intent intent = new Intent(JeansActivity.this, JeansDetailsActivity.class);
intent.putExtra(Titulo, nombre);
intent.putExtra(Marca, brand);
intent.putExtra(Colour, color);
intent.putExtra(Tipo, tipo);
intent.putExtra(Referencia, ref);
intent.putExtra("image", bitmap);
intent.putExtra("image2", bitmap2);
intent.putExtra("image3", bitmap3);
startActivity(intent);
}
});
}
我添加了GridAdapter
以及GridView及其内容(activity_gridjeans.xml & grid_row.xml)
的布局。
如果需要更多信息,请告诉我,或者您可以在GitHub [^]
中查看我的项目感谢帮助,
谢谢!
答案 0 :(得分:1)
更改
setContentView(R.layout.activity_jeans);
到
setContentView(R.layout.activity_gridjeans);
这是您的activity_jeans.xml
。看它有ListView
而非GridView
。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".VeroxActivity"
android:background="#ff0a393d">
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/list_divider2"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_row_selector" />
</RelativeLayout>
答案 1 :(得分:0)
您的布局activity_jeans.xml
没有名称为gridView
的视图
findViewById(R.id.gridView);
返回null
。