android 工作室 java setText 方法出现 NullPointerException
android studio java NullPointerException on setText method
我有问题,从昨天开始搜索,我post代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_wishes);
mListView = (ListView) findViewById(R.id.listId);
refreshData();
}
private void refreshData() {
wishes.clear();
dbHandler = new DatabaseHandler(getApplicationContext());
ArrayList<MyWishes> wishesFromDB = dbHandler.getWishes();
for (int i = 0; i < wishesFromDB.size(); i++){
String title = wishesFromDB.get(i).getTitle();
String content = wishesFromDB.get(i).getContent();
String date = wishesFromDB.get(i).getRecordDate();
MyWishes myWishes = new MyWishes();
myWishes.setTitle(title);
myWishes.setContent(content);
myWishes.setRecordDate(date);
wishes.add(myWishes);
}
dbHandler.close();
mWishAdapter = new WishAdapter(showWishesActivity.this, R.layout.list_row, wishes);
mListView.setAdapter(mWishAdapter);
mWishAdapter.notifyDataSetChanged();
}
public class WishAdapter extends ArrayAdapter<MyWishes>{
Activity activity;
MyWishes wish;
ArrayList<MyWishes> mData = new ArrayList<>();
int layoutRessource;
public WishAdapter(Activity act, int resource, ArrayList<MyWishes> data) {
super(act, resource, data);
activity = act;
layoutRessource = resource;
mData = data;
notifyDataSetChanged();
}
@Override
public int getCount() {
return mData.size();
}
@Override
public int getPosition(MyWishes item) {
return super.getPosition(item);
}
@Nullable
@Override
public MyWishes getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null || (row.getTag()) == null){
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutRessource, null);
holder = new ViewHolder();
holder.mTitle = (TextView) findViewById(R.id.titre);
holder.mDate = (TextView) findViewById(R.id.date);
row.setTag(holder);
}else {
holder = (ViewHolder) row.getTag();
}
holder.mMyWishes = getItem(position);
holder.mTitle.setText(holder.mMyWishes.getTitle());
holder.mDate.setText(holder.mMyWishes.getRecordDate());
return row;
}
class ViewHolder{
MyWishes mMyWishes;
TextView mTitle;
TextView mContent;
TextView mDate;
TextView mId;
}
}
在 getView 方法上,我在 holder.setText 上得到一个 NullPointerException
。`
我也post xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="12dp"
android:background="#90f96666">
<ImageView
android:id="@+id/imageViewDatabaseId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_agenda"/>
<TextView
android:id="@+id/titre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The title"
android:textStyle="bold"
android:layout_toRightOf="@+id/imageViewDatabaseId"
android:layout_marginLeft="20dp"/>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The detail"
android:layout_toRightOf="@+id/imageViewDatabaseId"
android:layout_below="@+id/titleId"
android:layout_marginTop="3dp"
android:layout_marginLeft="40dp"
android:textStyle="italic"/>
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at wishlist.yassine.com.mywishlist.showWishesActivity$WishAdapter.getView(showWishesActivity.java:133)
我不明白这个问题,任何人都可以帮助我,谢谢你的时间。
而不是这个
holder.mTitle = (TextView) findViewById(R.id.titre);
试试这个
holder.mTitle = (TextView) convertView.findViewById(R.id.titre);
if (row == null || (row.getTag()) == null){
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutRessource, null);
holder = new ViewHolder();
holder.mTitle = (TextView) row.findViewById(R.id.titre);
holder.mDate = (TextView) row.findViewById(R.id.date);
row.setTag(holder);
}
您的视图未在 getView
中初始化。所以:
删除这个:
holder.mTitle = (TextView) findViewById(R.id.titre);
holder.mDate = (TextView) findViewById(R.id.date);
有了这个:
holder.mTitle = (TextView) row.findViewById(R.id.titre);
holder.mDate = (TextView) row.findViewById(R.id.date);
在你的 getView 中使用这个:
holder.mTitle = (TextView)row.findViewById(R.id.titre);
holder.mDate = (TextView)row.findViewById(R.id.date);
您错过了可供参考的视图
listview
将每个项目替换为您指定的布局
然后 getview
方法在 return 视图 listview
项
你可以使用 like
holder.mTitle = (TextView) row.findViewById(R.id.titre);
行是布局视图
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null || (row.getTag()) == null){
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutRessource, null);
holder = new ViewHolder();
holder.mTitle = (TextView) row.findViewById(R.id.titre);
holder.mDate = (TextView) row.findViewById(R.id.date);
row.setTag(holder);
}else {
holder = (ViewHolder) row.getTag();
}
holder.mMyWishes = getItem(position);
holder.mTitle.setText(holder.mMyWishes.getTitle());
holder.mDate.setText(holder.mMyWishes.getRecordDate());
return row;
}
class ViewHolder{
MyWishes mMyWishes;
TextView mTitle;
TextView mContent;
TextView mDate;
TextView mId;
}
我有问题,从昨天开始搜索,我post代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_wishes);
mListView = (ListView) findViewById(R.id.listId);
refreshData();
}
private void refreshData() {
wishes.clear();
dbHandler = new DatabaseHandler(getApplicationContext());
ArrayList<MyWishes> wishesFromDB = dbHandler.getWishes();
for (int i = 0; i < wishesFromDB.size(); i++){
String title = wishesFromDB.get(i).getTitle();
String content = wishesFromDB.get(i).getContent();
String date = wishesFromDB.get(i).getRecordDate();
MyWishes myWishes = new MyWishes();
myWishes.setTitle(title);
myWishes.setContent(content);
myWishes.setRecordDate(date);
wishes.add(myWishes);
}
dbHandler.close();
mWishAdapter = new WishAdapter(showWishesActivity.this, R.layout.list_row, wishes);
mListView.setAdapter(mWishAdapter);
mWishAdapter.notifyDataSetChanged();
}
public class WishAdapter extends ArrayAdapter<MyWishes>{
Activity activity;
MyWishes wish;
ArrayList<MyWishes> mData = new ArrayList<>();
int layoutRessource;
public WishAdapter(Activity act, int resource, ArrayList<MyWishes> data) {
super(act, resource, data);
activity = act;
layoutRessource = resource;
mData = data;
notifyDataSetChanged();
}
@Override
public int getCount() {
return mData.size();
}
@Override
public int getPosition(MyWishes item) {
return super.getPosition(item);
}
@Nullable
@Override
public MyWishes getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null || (row.getTag()) == null){
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutRessource, null);
holder = new ViewHolder();
holder.mTitle = (TextView) findViewById(R.id.titre);
holder.mDate = (TextView) findViewById(R.id.date);
row.setTag(holder);
}else {
holder = (ViewHolder) row.getTag();
}
holder.mMyWishes = getItem(position);
holder.mTitle.setText(holder.mMyWishes.getTitle());
holder.mDate.setText(holder.mMyWishes.getRecordDate());
return row;
}
class ViewHolder{
MyWishes mMyWishes;
TextView mTitle;
TextView mContent;
TextView mDate;
TextView mId;
}
}
在 getView 方法上,我在 holder.setText 上得到一个 NullPointerException
。`
我也post xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="12dp"
android:background="#90f96666">
<ImageView
android:id="@+id/imageViewDatabaseId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_agenda"/>
<TextView
android:id="@+id/titre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The title"
android:textStyle="bold"
android:layout_toRightOf="@+id/imageViewDatabaseId"
android:layout_marginLeft="20dp"/>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The detail"
android:layout_toRightOf="@+id/imageViewDatabaseId"
android:layout_below="@+id/titleId"
android:layout_marginTop="3dp"
android:layout_marginLeft="40dp"
android:textStyle="italic"/>
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at wishlist.yassine.com.mywishlist.showWishesActivity$WishAdapter.getView(showWishesActivity.java:133)
我不明白这个问题,任何人都可以帮助我,谢谢你的时间。
而不是这个
holder.mTitle = (TextView) findViewById(R.id.titre);
试试这个
holder.mTitle = (TextView) convertView.findViewById(R.id.titre);
if (row == null || (row.getTag()) == null){
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutRessource, null);
holder = new ViewHolder();
holder.mTitle = (TextView) row.findViewById(R.id.titre);
holder.mDate = (TextView) row.findViewById(R.id.date);
row.setTag(holder);
}
您的视图未在 getView
中初始化。所以:
删除这个:
holder.mTitle = (TextView) findViewById(R.id.titre);
holder.mDate = (TextView) findViewById(R.id.date);
有了这个:
holder.mTitle = (TextView) row.findViewById(R.id.titre);
holder.mDate = (TextView) row.findViewById(R.id.date);
在你的 getView 中使用这个:
holder.mTitle = (TextView)row.findViewById(R.id.titre);
holder.mDate = (TextView)row.findViewById(R.id.date);
您错过了可供参考的视图
listview
将每个项目替换为您指定的布局
然后 getview
方法在 return 视图 listview
项
你可以使用 like
holder.mTitle = (TextView) row.findViewById(R.id.titre);
行是布局视图
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null || (row.getTag()) == null){
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutRessource, null);
holder = new ViewHolder();
holder.mTitle = (TextView) row.findViewById(R.id.titre);
holder.mDate = (TextView) row.findViewById(R.id.date);
row.setTag(holder);
}else {
holder = (ViewHolder) row.getTag();
}
holder.mMyWishes = getItem(position);
holder.mTitle.setText(holder.mMyWishes.getTitle());
holder.mDate.setText(holder.mMyWishes.getRecordDate());
return row;
}
class ViewHolder{
MyWishes mMyWishes;
TextView mTitle;
TextView mContent;
TextView mDate;
TextView mId;
}