Android: Attempt to invoke virtual method amp;#39;void android.widget.ListView.setAdapter(android.widget.ListAdapter)amp;#39; on a null object reference(Android:尝试在空对象引用上调用虚方法android.widget.ListView.setAdapter(android.widget.ListAdapter)amp;#39;) - IT屋-程序
问题描述
我正在尝试创建一个包含另一个活动中的图片和文本的列表视图。我不太专注于Android编程,所以基本上就像Android编程的新手,我在调试我的应用程序时遇到了困难。请帮帮我:)
错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.fingervoiz.app.tamla.Activity.ListDataIdntActivity.onCreate(ListDataIdntActivity.java:53)
完整的日志目录:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.fingervoiz.app.tamla, PID: 14968
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fingervoiz.app.tamla/com.fingervoiz.app.tamla.Activity.ListDataIdntActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.fingervoiz.app.tamla.Activity.ListDataIdntActivity.onCreate(ListDataIdntActivity.java:53)
at android.app.Activity.performCreate(Activity.java:6092)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
我的代码:(适配器)
public class ListDIDadapter extends ArrayAdapter<TamlaDataIdntModel> {
Context context;
int layoutResID;
ArrayList<TamlaDataIdntModel> dataToList = new ArrayList<TamlaDataIdntModel>();
public ListDIDadapter(Context context, int layoutResID, ArrayList<TamlaDataIdntModel> dataToList){
super(context,layoutResID,dataToList);
this.layoutResID = layoutResID;
this.context = context;
this.dataToList = dataToList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
Holder holder = null;
if (view == null){
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(layoutResID,parent,false);
holder = new Holder();
holder.txtFname = (TextView) view.findViewById(R.id.did_item_fullname);
holder.txtGender = (TextView) view.findViewById(R.id.did_item_gender);
holder.txtBplace = (TextView) view.findViewById(R.id.did_item_bplace);
holder.txtBday = (TextView) view.findViewById(R.id.did_item_bday);
holder.txtAddress = (TextView) view.findViewById(R.id.did_item_address);
holder.imgProfile = (ImageView) view.findViewById(R.id.did_item_profilepic);
view.setTag(holder);
}else {
holder = (Holder) view.getTag();
}
//FILL
TamlaDataIdntModel dids = dataToList.get(position);
if(dids != null){
holder.txtFname.setText(dids.get_lname()+", "+dids.get_fname()+" "+dids.get_mname());
holder.txtGender.setText(dids.get_gender());
holder.txtBplace.setText(dids.get_bplace());
holder.txtBday.setText(dids.get_bday());
holder.txtAddress.setText(dids.get_address());
byte[] outImage = dids.get_dp();
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
holder.imgProfile.setImageBitmap(theImage);
}
return view;
}
static class Holder{
TextView txtFname;
TextView txtGender;
TextView txtBplace;
TextView txtBday;
TextView txtAddress;
ImageView imgProfile;
}
}
(列出活动)
public class ListDataIdntActivity extends Activity{
public static final String TAG = "ListDataIdntActivity";
public static final int REQUEST_CODE_ADD_COMPANY = 40;
public static final String EXTRA_ADDED_COMPANY = "extra_key_added_did";
private ListView mListViewDID;
private TextView emptyTxt;
private Button b1;
private ListDIDadapter adapter;
private ArrayList<TamlaDataIdntModel> arryList;
private DataIdntDAO mDIDdao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Initialize View
mListViewDID = (ListView) findViewById(R.id.list_did);
emptyTxt = (TextView) findViewById(R.id.txt_empty_list_dids);
b1 = (Button) findViewById(R.id.back_list_did);
//Fill List View
mDIDdao = new DataIdntDAO(this);
List<TamlaDataIdntModel> mIdntList = mDIDdao.getAllDID();
if (mIdntList != null && !mIdntList.isEmpty()){
adapter = new ListDIDadapter(this,R.layout.activity_list_did,arryList);
mListViewDID.setAdapter(adapter);
}else {
emptyTxt.setVisibility(View.VISIBLE);
mListViewDID.setVisibility(View.GONE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_CODE_ADD_COMPANY){
if(requestCode == RESULT_OK){
// add the added identity to the list and refresh the list view
if(data != null){
TamlaDataIdntModel createdNewDID = (TamlaDataIdntModel)data.getSerializableExtra(EXTRA_ADDED_COMPANY);
if(createdNewDID != null){
if(arryList == null){
arryList = new ArrayList<TamlaDataIdntModel>();
arryList.add(createdNewDID);
if (adapter == null){
if (mListViewDID.getVisibility() != View.VISIBLE){
mListViewDID.setVisibility(View.VISIBLE);
emptyTxt.setVisibility(View.GONE);
}
adapter = new ListDIDadapter(this,R.layout.activity_list_did,arryList);
mListViewDID.setAdapter(adapter);
}else {
if (arryList != null){
for (Object object : arryList) {
adapter.insert((TamlaDataIdntModel) object, adapter.getCount());
}
}
adapter.notifyDataSetChanged();
}
}
}
}
}
} else {
super.onActivityResult(requestCode,resultCode,data);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mDIDdao.close();
}
}
推荐答案
您遗漏了一行非常重要的行-setContentView()
。该行将XML与活动的视图绑定在一起,它是用于findViewById
的视图。将此行添加到您的onCreate:
@Override
public void onCreate(Bundle state){
super.onCreate(state);
setContentView(R.layout.name_of_the_xml);
这篇关于Android:尝试在空对象引用上调用虚方法android.widget.ListView.setAdapter(android.widget.ListAdapter)&;#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android:尝试在空对象引用上调用虚方法android.widget.ListView.setAdapter(android.widget.ListAdapter)&;#39;
基础教程推荐
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01