Android:尝试在空对象引用上调用虚拟方法“void android.widget.ListView.setAdapter(android.widget.ListAdapter)”
Posted
技术标签:
【中文标题】Android:尝试在空对象引用上调用虚拟方法“void android.widget.ListView.setAdapter(android.widget.ListAdapter)”【英文标题】:Android: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference 【发布时间】:2016-11-12 05:18:03 【问题描述】:我正在尝试在另一个活动中创建一个包含图片和文本的列表视图。我不太专注于 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)
完整的 LogCat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.fingervoiz.app.tamla, PID: 14968
java.lang.RuntimeException: Unable to start activity ComponentInfocom.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();
【问题讨论】:
【参考方案1】:您错过了一条非常重要的线路 - setContentView()
。该行将 xml 与 Activity 的视图绑定在一起,它是用于findViewById
的视图。将此行添加到您的 onCreate:
@Override
public void onCreate(Bundle state)
super.onCreate(state);
setContentView(R.layout.name_of_the_xml);
【讨论】:
谢谢,我找到了。但是,出现了一个新问题-.-但我会解决它感谢您的帮助 没问题。如果它解决了您最初的问题,请考虑接受答案。什么是新的,如果不是太长,我可以尝试在 cmets 中提供帮助。如果是,如果您无法自行解决,请提出一个新问题。 谢谢,:) 出现的新问题是,我的数据库中没有显示任何数据。但我的数据库里面有项目 呃,这可能是一百万件事情。首先调试数据丢失的位置(首先是您的查询错误,然后是其余的)。如果您无法处理,请发布新问题。 @Vucko 先生,您能帮我如何获取数据库中新插入的 id 以及如何实现它。我已经阅读了一些与此相关的内容,但我不知道从哪里开始【参考方案2】:你必须在使用findViewById()
之前添加MainActivity
的布局(包含Listview
)
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContent(R.layout_MAIN_LAYOUT_ACTIVITY);
//Initialize View
mListViewDID = (ListView) findViewById(R.id.list_did);
【讨论】:
以上是关于Android:尝试在空对象引用上调用虚拟方法“void android.widget.ListView.setAdapter(android.widget.ListAdapter)”的主要内容,如果未能解决你的问题,请参考以下文章
尝试在空对象引用上调用虚拟方法 'android.graphics.Rect android.graphics.drawable.Drawable.getBounds()'
尝试在空对象引用上调用虚拟方法 'android.view.Window$Callback android.view.Window.getCallback()'
尝试在空对象引用上调用虚拟方法 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()'
尝试在空对象引用上调用虚拟方法 'void android.view.View.measure(int, int)'