使用 ListView 截取图像到另一个 activity: java.lang.RuntimeException: 无法启动 activity ComponentInfo: java.lang.NullPointerException
Using ListView volley image to another activity: java.lang.RuntimeException: Unable to start activity ComponentInfo: java.lang.NullPointerException
一直在尝试将一些图像和文本从我的 Volley
ListView
传递到另一个 activity,但图像出现了一些问题。当我只传递文本时,它显示没有问题,但是一旦我插入代码以提取图像,我就会在 logcat
:
中收到以下错误
03-04 22:24:05.185 368-368/com.wlodsgn.bunbunup E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.wlodsgn.bunbunup, PID: 368
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wlodsgn.bunbunup/com.wlodsgn.bunbunup.DetailsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
at android.app.ActivityThread.access0(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.wlodsgn.bunbunup.DetailsActivity.onCreate(DetailsActivity.java:39)
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.access0(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)
错误看起来像是依赖于我的 DetailsActivity.java
下的以下部分:
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
String bitmap = i.getStringExtra("images");
NetworkImageView thumbNail = (NetworkImageView) findViewById(R.id.thumbnail);
thumbNail.setImageUrl(bitmap, imageLoader);`
我试过用这个看看能不能解决问题,但是没有成功。
这里我 post 我的完整 DetailsActivity.java 和 VeroxJeansActivity.java 如果需要的话。
如果需要,我会添加更多代码。感谢任何帮助
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.TextView;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.ImageLoader;
import com.wlodsgn.bunbunup.app.AppController;
/**
* Created by WiLo on 3/4/2015.
*/
public class DetailsActivity extends ActionBarActivity {
private static String Titulo="titulo";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
/**getSupportActionBar().hide();**/
//Back button
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Intent i=getIntent();
String name = i.getStringExtra(Titulo);
TextView lblName = (TextView) findViewById(R.id.titulo);
lblName.setText(name);
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
String bitmap = i.getStringExtra("images");
NetworkImageView thumbNail = (NetworkImageView) findViewById(R.id.thumbnail);
thumbNail.setImageUrl(bitmap, imageLoader);
}
public void onClickHandler(View v){
switch(v.getId()){
case R.id.thumbnail:
startActivity(new Intent(this,VeroxJeansActivity.class));
}
}
}
---------------------------------------- --
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import com.wlodsgn.bunbunup.adapter.CustomListAdapter;
import com.wlodsgn.bunbunup.app.AppController;
import com.wlodsgn.bunbunup.model.VeroxJeans;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
/**
* Created by WiLo on 2/27/2015.
*/
public class VeroxJeansActivity extends ActionBarActivity {
/**@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_veroxjeans);
}**/
// Log tag
private static final String TAG = VeroxJeansActivity.class.getSimpleName();
// Verox Jeans json url
private static final String url = "http://wlodsgn.x10host.com/json/vrxjns.json";
private ProgressDialog pDialog;
private List<VeroxJeans> veroxJeansList = new ArrayList<VeroxJeans>();
private ListView listView;
private CustomListAdapter adapter;
private static String Titulo="titulo";
private static String bitmap="thumbnailUrl";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_veroxjeans);
//Back button
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, veroxJeansList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#1b1b1b")));
// Creating volley request obj
JsonArrayRequest veroxJeansReq = 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);
VeroxJeans veroxjeans = new VeroxJeans();
veroxjeans.setTitulo(obj.getString("titulo"));
veroxjeans.setThumbnailUrl(obj.getString("image"));
veroxjeans.setMarca(obj.getString("marca"));
veroxjeans.setColor(obj.getString("color"));
veroxjeans.setTipo(obj.getString("tipo"));
veroxjeans.setRef(obj.getInt("ref"));
// adding verox jeans to verox jeans array
veroxJeansList.add(veroxjeans);
} 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(veroxJeansReq);**/
AppController.getInstance().addToRequestQueue(veroxJeansReq);
listView.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();
bitmap = ((VeroxJeans)veroxJeansList.get(position)).getThumbnailUrl();
Intent intent = new Intent(VeroxJeansActivity.this, DetailsActivity.class);
intent.putExtra(Titulo, nombre);
intent.putExtra("images", bitmap);
startActivity(intent);
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
/**@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}**/
}
答案在我 post 的最后一条评论中 post。需要在我的 .xml 文件的 ID 中将 thumbNail
改回 thumbnail
。
一直在尝试将一些图像和文本从我的 Volley
ListView
传递到另一个 activity,但图像出现了一些问题。当我只传递文本时,它显示没有问题,但是一旦我插入代码以提取图像,我就会在 logcat
:
03-04 22:24:05.185 368-368/com.wlodsgn.bunbunup E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.wlodsgn.bunbunup, PID: 368
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wlodsgn.bunbunup/com.wlodsgn.bunbunup.DetailsActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
at android.app.ActivityThread.access0(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.wlodsgn.bunbunup.DetailsActivity.onCreate(DetailsActivity.java:39)
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.access0(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)
错误看起来像是依赖于我的 DetailsActivity.java
下的以下部分:
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
String bitmap = i.getStringExtra("images");
NetworkImageView thumbNail = (NetworkImageView) findViewById(R.id.thumbnail);
thumbNail.setImageUrl(bitmap, imageLoader);`
我试过用这个
这里我 post 我的完整 DetailsActivity.java 和 VeroxJeansActivity.java 如果需要的话。
如果需要,我会添加更多代码。感谢任何帮助
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.TextView;
import com.android.volley.toolbox.Volley;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.ImageLoader;
import com.wlodsgn.bunbunup.app.AppController;
/**
* Created by WiLo on 3/4/2015.
*/
public class DetailsActivity extends ActionBarActivity {
private static String Titulo="titulo";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
/**getSupportActionBar().hide();**/
//Back button
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Intent i=getIntent();
String name = i.getStringExtra(Titulo);
TextView lblName = (TextView) findViewById(R.id.titulo);
lblName.setText(name);
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
String bitmap = i.getStringExtra("images");
NetworkImageView thumbNail = (NetworkImageView) findViewById(R.id.thumbnail);
thumbNail.setImageUrl(bitmap, imageLoader);
}
public void onClickHandler(View v){
switch(v.getId()){
case R.id.thumbnail:
startActivity(new Intent(this,VeroxJeansActivity.class));
}
}
}
---------------------------------------- --
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import com.wlodsgn.bunbunup.adapter.CustomListAdapter;
import com.wlodsgn.bunbunup.app.AppController;
import com.wlodsgn.bunbunup.model.VeroxJeans;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
/**
* Created by WiLo on 2/27/2015.
*/
public class VeroxJeansActivity extends ActionBarActivity {
/**@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_veroxjeans);
}**/
// Log tag
private static final String TAG = VeroxJeansActivity.class.getSimpleName();
// Verox Jeans json url
private static final String url = "http://wlodsgn.x10host.com/json/vrxjns.json";
private ProgressDialog pDialog;
private List<VeroxJeans> veroxJeansList = new ArrayList<VeroxJeans>();
private ListView listView;
private CustomListAdapter adapter;
private static String Titulo="titulo";
private static String bitmap="thumbnailUrl";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_veroxjeans);
//Back button
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, veroxJeansList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// changing action bar color
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#1b1b1b")));
// Creating volley request obj
JsonArrayRequest veroxJeansReq = 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);
VeroxJeans veroxjeans = new VeroxJeans();
veroxjeans.setTitulo(obj.getString("titulo"));
veroxjeans.setThumbnailUrl(obj.getString("image"));
veroxjeans.setMarca(obj.getString("marca"));
veroxjeans.setColor(obj.getString("color"));
veroxjeans.setTipo(obj.getString("tipo"));
veroxjeans.setRef(obj.getInt("ref"));
// adding verox jeans to verox jeans array
veroxJeansList.add(veroxjeans);
} 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(veroxJeansReq);**/
AppController.getInstance().addToRequestQueue(veroxJeansReq);
listView.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();
bitmap = ((VeroxJeans)veroxJeansList.get(position)).getThumbnailUrl();
Intent intent = new Intent(VeroxJeansActivity.this, DetailsActivity.class);
intent.putExtra(Titulo, nombre);
intent.putExtra("images", bitmap);
startActivity(intent);
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
/**@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}**/
}
答案在我 post 的最后一条评论中 post。需要在我的 .xml 文件的 ID 中将 thumbNail
改回 thumbnail
。