我在ListView
中充气4件事1.图像
2.Text
3.Text
4.Text (空指针异常)
带有自定义适配器的主类文件...
来源:
ListView list_hashtag=null;
private HashTag_Result mhashtag;
private HashTag_Result_Adapter adapter;
private ArrayList<HashTag_Result_Data > arr_list_data=new ArrayList<HashTag_Result_Data>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hash_tag_result);
list_hashtag=(ListView)findViewById(R.id.listview_hashtag);
Resources res=getResources();
setListData();
mhashtag=this;
adapter=new HashTag_Result_Adapter(mhashtag,arr_list_data,res);
list_hashtag.setAdapter(adapter);
}
private void setListData() {
DateFormat dformat=new SimpleDateFormat("dd-MM-yyyy");
Date date=new Date();
for(int i=0;i<30; i++)
{
final HashTag_Result_Data result_data=new HashTag_Result_Data();
result_data.setImage("image" +i);
result_data.setName("User " +i);
result_data.setStatus("Lorem Lipsum is simply dummy text in typecasting and printing industry");
result_data.setDate("Date is " +dformat.format(date));
arr_list_data.add(result_data);
}
}
public void onItemClick(int position)
{
HashTag_Result_Data temp_values=(HashTag_Result_Data)arr_list_data.get(position);
Intent i=new Intent(getApplicationContext(),HashTag_Snap_Of_Day.class);
startActivity(i);
overridePendingTransition(R.anim.animation, R.anim.animation2);
finish();
}
CustomAdapter:
public class HashTag_Result_Adapter extends BaseAdapter implements OnClickListener {
Activity mactivity;
Resources res1;
ArrayList arr_list;
int i=0;
private static LayoutInflater l_inflater;
HashTag_Result_Data result_data=null;
public HashTag_Result_Adapter(Activity a,ArrayList arr_data,Resources res2 )
{
mactivity=a;
arr_list=arr_data;
res1=res2;
l_inflater=(LayoutInflater)mactivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
if(arr_list.size()<=0)
return 1;
return arr_list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public static class ViewHolder
{
public ImageView image1;
public TextView text1;
public TextView text2;
public TextView text3;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null)
{
vi=l_inflater.inflate(R.layout.hashtag_result_listview_item, null);
holder=new ViewHolder();
holder.image1=(ImageView)vi.findViewById(R.id.imageview_hashtag_result_item1);
holder.text1=(TextView)vi.findViewById(R.id.TextView_hashtag_result_item2);
holder.text2=(TextView)vi.findViewById(R.id.TextView_hashtag_result_item3);
holder.text3=(TextView)vi.findViewById(R.id.TextView_hashtag_result_item4);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
if(arr_list.size()<=0)
{
holder.text1.setText("Currently no item ");
}
else
{
result_data=null;
result_data=(HashTag_Result_Data)arr_list.get(position);
holder.image1.setImageResource(res1.getIdentifier("com.example.youthchat:drawable/"+result_data.getImage(),null,null));
holder.text1.setText(result_data.getName());
holder.text2.setText(result_data.getStatus());
holder.text3.setText(result_data.getDate());
vi.setOnClickListener(new OnItemClickListener(position));
}
return vi;
}
@Override
public void onClick(View v) {
}
private class OnItemClickListener implements OnClickListener{
private int mpostion;
OnItemClickListener(int position)
{
mpostion=position;
}
@Override
public void onClick(View v) {
MainActivity mact=(MainActivity)mactivity;
mact.onItemClick(mpostion);
}
}
}
logcat的:
03-06 18:43:28.548: D/AndroidRuntime(14888): Shutting down VM
03-06 18:43:28.548: W/dalvikvm(14888): threadid=1: thread exiting with uncaught exception (group=0x40c341f8)
03-06 18:43:28.548: E/AndroidRuntime(14888): FATAL EXCEPTION: main
03-06 18:43:28.548: E/AndroidRuntime(14888): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.youthchat/com.example.youthchat.HashTag_Result}: java.lang.NullPointerException
03-06 18:43:28.548: E/AndroidRuntime(14888): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
03-06 18:43:28.548: E/AndroidRuntime(14888): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
03-06 18:43:28.548: E/AndroidRuntime(14888): at android.app.ActivityThread.access$600(ActivityThread.java:127)
03-06 18:43:28.548: E/AndroidRuntime(14888): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
03-06 18:43:28.548: E/AndroidRuntime(14888): at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 18:43:28.548: E/AndroidRuntime(14888): at android.os.Looper.loop(Looper.java:137)
03-06 18:43:28.548: E/AndroidRuntime(14888): at android.app.ActivityThread.main(ActivityThread.java:4507)
03-06 18:43:28.548: E/AndroidRuntime(14888): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 18:43:28.548: E/AndroidRuntime(14888): at java.lang.reflect.Method.invoke(Method.java:511)
03-06 18:43:28.548: E/AndroidRuntime(14888): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:978)
03-06 18:43:28.548: E/AndroidRuntime(14888): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:745)
03-06 18:43:28.548: E/AndroidRuntime(14888): at dalvik.system.NativeStart.main(Native Method)
03-06 18:43:28.548: E/AndroidRuntime(14888): Caused by: java.lang.NullPointerException
03-06 18:43:28.548: E/AndroidRuntime(14888): at com.example.youthchat.HashTag_Result.onCreate(HashTag_Result.java:34)
03-06 18:43:28.548: E/AndroidRuntime(14888): at android.app.Activity.performCreate(Activity.java:4469)
03-06 18:43:28.548: E/AndroidRuntime(14888): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
03-06 18:43:28.548: E/AndroidRuntime(14888): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
03-06 18:43:28.548: E/AndroidRuntime(14888): ... 11 more
03-06 18:43:28.578: D/dalvikvm(14888): GC_CONCURRENT freed 774K, 13% free 8277K/9479K, paused 1ms+2ms
答案 0 :(得分:0)
您正在尝试将Activity
转换为HashTag_Result
并转而使用适配器而应该
adapter=new HashTag_Result_Adapter(this,arr_list_data,res);
答案 1 :(得分:0)
最后我解决了这个问题并从紧张局势中解脱出来......
这是问题......
list_hashtag=(ListView)findViewById(R.id.listview_hashtag);
实际上问题是重复ID ... 现在我在xml中添加了新的id ...
list_hashtag=(ListView)findViewById(R.id.listview_hashtag_result);
并且工作正常......