我有一个从 web 服务获得的 listview 数据,当我单击 listview 项目时,它会进入详细的 Activity,当我在 nextbutton 上单击时,它应该将我带到列表中的下一个项目,类似地单击 prebutton 它应该带我到列表中的上一个项目,但是现在当我单击下一个/上一个按钮时,我正在强制关闭,我正在使用 BaseAdapter。以下是我的代码
详细活动
public class SampleDesp extends Activity {
ArrayList<HashMap<String, String>> songsList;
ListView list;
// JSON node keys
static final String KEY_URL_FOR_MAP = "url_site";
private static final String KEY_TITLE = "title";
private static final String KEY_SITEURL = "url";
private static final String KEY_DATE = "date";
private static final String KEY_NAME = "name";
private static final String KEY_CONTENT = "content";
private static final String KEY_URL = "url";
static final String KEY_SLUG1= "slug";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.sampledes);
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
// move up event handler
ImageButton preButton = (ImageButton) findViewById(R.id.prevButton);
preButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
movePre();
}
});
// move down event handler
ImageButton nxtButton = (ImageButton) findViewById(R.id.nextButton);
nxtButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
moveNxt();
}
});
ListView list = new ListView(this);
StationAdapter adapter1 = new StationAdapter(this, songsList);
list.setAdapter(adapter1);
// getting intent data
Intent in = getIntent();
final String url1 = in.getStringExtra(KEY_URL);
ImageView imgv = (ImageView) findViewById(R.id.imgdesc);
ImageLoader imageLoader = new ImageLoader(getApplicationContext());
imageLoader.DisplayImage(url1, imgv);
// Get JSON values from previous intent
String songsList1 = in.getStringExtra("song");
int listPosition = in.getIntExtra("listPosition", 0);
final String title = in.getStringExtra(KEY_TITLE);
final String siteurl = in.getStringExtra(KEY_URL_FOR_MAP);
String date = in.getStringExtra(KEY_DATE);
String name = in.getStringExtra(KEY_NAME);
final String content = in.getStringExtra(KEY_CONTENT);
// Displaying all values on the screen
TextView lblName = (TextView) findViewById(R.id.name_label);
TextView lblUrl = (TextView) findViewById(R.id.url_label);
TextView lblCost = (TextView) findViewById(R.id.email_label);
TextView lblDesc = (TextView) findViewById(R.id.mobile_label);
TextView lblCont = (TextView) findViewById(R.id.content_label);
lblName.setText(title);
lblUrl.setText(siteurl);
lblCost.setText(date);
lblDesc.setText(name);
lblCont.setText(content);
}
private void movePre(){
list.setSelection(list.getSelectedItemPosition() - 1);
}
// Move selected item "down" in the ViewList.
private void moveNxt(){
list.setSelection(list.getSelectedItemPosition() + 1);
}
站适配器
class StationAdapter extends BaseAdapter{
private static final String TAG = null;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public StationAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
Log.v(TAG, "Message here");
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.myfragment_layout, null);
TextView title = (TextView)vi.findViewById(R.id.name_label); // title
TextView siteurl = (TextView)vi.findViewById(R.id.url_label);
TextView date = (TextView)vi.findViewById(R.id.email_label); // artist name
TextView content = (TextView)vi.findViewById(R.id.content_label); // duration
TextView name = (TextView)vi.findViewById(R.id.mobile_label);
// duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.imgdesc); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
ListView list;
// Setting all values in listview
title.setText(song.get(MainActivity.KEY_TITLE));
// siteurl.setText(song.get(MainActivity.KEY_SITEURL));
date.setText(song.get(MainActivity.KEY_DATE));
siteurl.setText(song.get(MainActivity.KEY_URL_FOR_MAP));// correct code
((TextView)siteurl).setVisibility(TextView.INVISIBLE);
content.setText(song.get(MainActivity.KEY_CONTENT));
name.setText(song.get(MainActivity.KEY_NAME));
System.out.println("output: " +name);
imageLoader.DisplayImage(song.get(MainActivity.KEY_URL), thumb_image);
return vi;
}
}
LogCat 错误
02-23 12:32:52.506: E/AndroidRuntime(894): FATAL EXCEPTION: main
02-23 12:32:52.506: E/AndroidRuntime(894): java.lang.NullPointerException
02-23 12:32:52.506: E/AndroidRuntime(894): at com.example.sampleofmain.SampleDesp.moveNxt(SampleDesp.java:137)
02-23 12:32:52.506: E/AndroidRuntime(894): at com.example.sampleofmain.SampleDesp.access$1(SampleDesp.java:135)
02-23 12:32:52.506: E/AndroidRuntime(894): at com.example.sampleofmain.SampleDesp$2.onClick(SampleDesp.java:82)
02-23 12:32:52.506: E/AndroidRuntime(894): at android.view.View.performClick(View.java:2485)
02-23 12:32:52.506: E/AndroidRuntime(894): at android.view.View$PerformClick.run(View.java:9080)
02-23 12:32:52.506: E/AndroidRuntime(894): at android.os.Handler.handleCallback(Handler.java:587)
02-23 12:32:52.506: E/AndroidRuntime(894): at android.os.Handler.dispatchMessage(Handler.java:92)
02-23 12:32:52.506: E/AndroidRuntime(894): at android.os.Looper.loop(Looper.java:123)
02-23 12:32:52.506: E/AndroidRuntime(894): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-23 12:32:52.506: E/AndroidRuntime(894): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 12:32:52.506: E/AndroidRuntime(894): at java.lang.reflect.Method.invoke(Method.java:507)
02-23 12:32:52.506: E/AndroidRuntime(894): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-23 12:32:52.506: E/AndroidRuntime(894): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-23 12:32:52.506: E/AndroidRuntime(894): at dalvik.system.NativeStart.main(Native Method)