我已经在android中创建了自定义列表视图。我希望每当调用列表视图单击事件时,它就会进入另一个活动,并且该列表上的数据将显示在新活动上。下面的代码是我的Like MainActivity(这里命名为ListofWorkers),其中也包含布局文件。
package com.example.dispatchmedemo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import java.util.ArrayList;
import java.util.List;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.example.dispatchmedemo.CustomListViewAdapter;
import com.example.dispatchmedemo.RowItem;
public class ListofWorkers extends Activity {
public static final String[] name = new String[] { "ABC",
"YZA", "ASDSA", "ASD", "ASD", "SDFS" };
public static final String[] landmarks = new String[] {
"AFDDF",
"ADF", "ADFD",
"ADFAD", "ADFADF", "ADFAD" };
public static final String[] rates = new String[] {
"100",
"500", "300",
"400", "120", "600" };
public static final Integer[] images = { R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher };
ListView listView;
List<RowItem> rowItems;
Button responseSubmit;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listof_workers);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < name.length; i++) {
RowItem item = new RowItem(images[i], name[i], landmarks[i], rates[i]);
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_single, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent,
View view,int position, long id) {
Intent i = new Intent(ListofWorkers.this, WorkerProfile.class);
i.putExtra("name", name);
i.putExtra("landmarks", landmarks);
i.putExtra("rates", rates);
i.putExtra("image", images);
i.putExtra("position", position);
startActivity(i);
}
});
}
}
现在,这是我的另一个活动“ WorkerProfile”,在其中我得到了名称,地标和比率,但仅在图像中,我得到了空指针异常,这就是我在TOAST中所做的。
package com.example.dispatchmedemo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class WorkerProfile extends Activity {
// Declare Variables
TextView txtname;
TextView txtlandmark;
TextView txtrate;
ImageView imgpro;
String[] name;
String[] landmarks;
String[] rate;
int[] images;
int position;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_worker_profile);
Intent i = getIntent();
position = i.getExtras().getInt("position");
name = i.getStringArrayExtra("name");
landmarks = i.getStringArrayExtra("landmarks");
rate = i.getStringArrayExtra("rates");
images = i.getIntArrayExtra("image");
txtname = (TextView) findViewById(R.id.textView1);
txtlandmark = (TextView) findViewById(R.id.textView2);
txtrate = (TextView) findViewById(R.id.textView3);
imgpro = (ImageView)findViewById(R.id.ActivityWorkerProflePic);
txtname.setText(name[position]);
txtlandmark.setText(landmarks[position]);
txtrate.setText(rate[position]);
imgpro.setImageResource(images[position]);
}
}
最佳答案
在这里,您将整数作为由包装类Integer定义的对象进行传递。
在接收器活动中,您正在将其检索为原始类型(int)的数组。这就是为什么您会得到Null指针异常的原因。
请更改以下代码,
public static final Integer[] images = { R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher };
至,
public static final int[] images = { R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher };
希望会有所帮助。