java Android TextView SetText null 对象引用错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39565372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android TextView SetText null object reference error
提问by user1591668
I am new to android and I am getting a
我是 android 新手,我得到了一个
Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
error on my app. I have read other questions on here like this one - java.lang.NullPointerException - setText on null object referencebut I still get the same issue. My format is a little bit different as this is done in a Fragment and what I am doing is having a Custom Adapter from a listview. Here is my code
我的应用程序出错。我已经在这里阅读了其他问题,比如这个- java.lang.NullPointerException - setText on null object reference,但我仍然遇到同样的问题。我的格式有点不同,因为这是在 Fragment 中完成的,我正在做的是从列表视图中获得自定义适配器。这是我的代码
fragment_song_genre.xml
fragment_song_genre.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:bind="http://schemas.android.com/tools">
<data class="SongGenreBinding">
<variable
name="handler"
type="com.mgrmobi.joox.fragments.FilterSongGenreFragment" />
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">
<RelativeLayout
android:id="@+id/toprow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:background="@null"
android:onClick="@{ handler.onCloseClicked }"
android:src="@drawable/search_close" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="80dp"
android:text="Genre"
android:textColor="#fff"
android:textSize="20sp" />
<TextView
android:onClick="@{ handler.reset }"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:text="@string/reset"
android:textAllCaps="true"
android:textColor="#fff"
android:textSize="12sp" />
</RelativeLayout>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toprow"
android:layout_marginTop="24dp"></ListView>
</RelativeLayout>
That is the main page and it has a Listview in the Bottom and here is the fragment that calls that xml layout
这是主页面,底部有一个 Listview,这是调用该 xml 布局的片段
public class FilterSongGenreFragment extends BaseFragment {
private SongGenreBinding binding;
private ArrayList<String> genres;
String[] names= {"Johm","Paul","Mike"};
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
}
private static final String GENRES_KEY = "genres";
public static FilterSongGenreFragment newInstance(ArrayList<String> genres) {
Bundle args = new Bundle();
args.putStringArrayList(GENRES_KEY, genres);
FilterSongGenreFragment fragment = new FilterSongGenreFragment();
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_song_genre, container, false);
binding.setHandler(this);
genres = getArguments().getStringArrayList(GENRES_KEY);
initList();
return binding.getRoot();
}
private void initList() {
// ArrayAdapter adapter = new ArrayAdapter(getActivity(), R.layout.genre_list_text, android.R.id.text1, genres);
FilterSongsAdapter filterSongsAdapter= new FilterSongsAdapter(getActivity(),names);
binding.list.setAdapter(filterSongsAdapter);
binding.list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,int position, long l) {
((SongFilterListener) getActivity()).onGenreSelected(genres.get(position));
/*
textView = (ImageView) view.findViewById(R.id.selected_genre);
textView.setVisibility(View.VISIBLE);
*/
// close();
}
});
}
private void close() {
getActivity().getFragmentManager().beginTransaction().remove(this).commit();
}
public void onCloseClicked(View view) {
close();
}
public void reset(View view) {
((SongFilterListener) getActivity()).onResetFilters();
close();
}
}
Here is the xml for the listview custom style genre_list_text.xml
这是listview自定义样式genre_list_text.xml的xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|left"
android:text="Genre"
android:paddingBottom="16dp"
android:paddingLeft="4dp"
android:paddingTop="16dp"
android:textColor="#fff"
android:textSize="16sp" />
<ImageView
android:id="@+id/selected_genre"
android:layout_height="24dp"
android:layout_width="24dp"
android:visibility="invisible"
android:src="@drawable/check_circle"
android:layout_marginStart="140dp"
android:layout_centerVertical="true" />
</RelativeLayout>
and here is my adapter and as stated before the error happens in the SetText area
这是我的适配器,正如在 SetText 区域发生错误之前所述
public class FilterSongsAdapter extends ArrayAdapter<String> {
private Context c;
String[] names= {};
LayoutInflater inflater;
public FilterSongsAdapter(Context context,String[] names) {
super(context,R.layout.genre_list_text,names);
this.c=context;
this.names=names;
}
public static class ViewHolder {
TextView text1;
}
@Override
public View getView(int position,View convertView,ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.from(c).inflate(R.layout.genre_list_text, null);
// Initialize
holder.text1=(TextView) convertView.findViewById(R.id.text1);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.text1.setText(names[position]);
return convertView;
}
}
The SetText inside the GetView is throwing the exception every time, that TextView should have 3 objects names from the Names String Array which I have seen that it has. Based on the error I know that i'm setting the SetText before the FindByID method locates the TextView but I have moved things around and nothing works. Any help would be great . I also been following this tutorial for reference https://www.youtube.com/watch?v=99C_UpLcBRk. I do not know if maybe with a Fragment things are different.
GetView 中的 SetText 每次都抛出异常,TextView 应该有来自 Names String Array 的 3 个对象名称,我已经看到它有。基于错误,我知道我在 FindByID 方法定位 TextView 之前设置了 SetText,但我已经移动了一些东西,但没有任何效果。任何帮助都会很棒。我也一直在关注本教程以供参考https://www.youtube.com/watch?v=99C_UpLcBRk。我不知道是否可能与 Fragment 事情不同。
回答by LearnPainLess
in your layout you have given default id to TextView
android:id="@android:id/text1"
.
在您的布局中,您已将默认 ID 赋予TextView
android:id="@android:id/text1"
.
and in JAVA file you are giving R.id.text1
并在您提供的 JAVA 文件中 R.id.text1
make changes in xml or java.
在 xml 或 java 中进行更改。
ether change in xml from android:id="@android:id/text1"
to android:id="@+id/text1"
or in java android.R.id.text1
在 xml 中从android:id="@android:id/text1"
到android:id="@+id/text1"
或在 java 中的以太网更改android.R.id.text1
回答by W4R10CK
@Override
public View getView(int position,View convertView,ViewGroup parent) {
ViewHolder holder = new ViewHolder(); //because error says null object reference, it means object of TextView `text1` is not found.
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null){
convertView = inflater.inflate(R.layout.genre_list_text, null);
holder.text1=(TextView) convertView.findViewById(R.id.text1);
holder.text1.setText(names[position]);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
And from your provided XML
code, I can see you did not provided a proper id for TextView
. Change in XML
from android:id="@android:id/text1"
to android:id="@+id/text1"
.
从您提供的XML
代码中,我可以看到您没有为TextView
. XML
从更改android:id="@android:id/text1"
为android:id="@+id/text1"
。
回答by AbhayBohra
In your genre_list_text.xml change android:id="@android:id/text1" to android:id="@+id/text1"
在您的流派_list_text.xml 更改 android:id="@android:id/text1" 到 android:id="@+id/text1"
and in your getView Method
并在您的 getView 方法中
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.genre_list_text, parent, false);
viewHolder = new ViewHolder();
holder.text1=(TextView) view.findViewById(R.id.text1);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
holder.text1.setText(names[position]);
return view;
}