-3

我是新來的Android和我在我的應用程序得到一個的Android的TextView的setText空對象引用錯誤

Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 

錯誤。我已經閱讀了其中的其他問題,例如- java.lang.NullPointerException - setText on null object reference,但我仍然遇到同樣的問題。我的格式有點不同,因爲這是在Fragment中完成的,而我正在做的是從ListView中獲取自定義適配器。這裏是我的代碼

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> 

這是主頁,它在底部的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(); 
    } 

} 

這是列表視圖自定義樣式的xml genre_list_text.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> 

,這裏是我的適配器和的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; 
    } 




} 

的GetView內的的setText每一次拋出異常,這TextView的應該有3個對象的名稱爲聲明從我已經看到它的名稱字符串數組。基於這個錯誤,我知道我在FindByID方法定位TextView之前設置SetText,但我已經移動了東西並且沒有任何效果。任何幫助都會很棒。我也一直在關注本教程,以供參考https://www.youtube.com/watch?v=99C_UpLcBRk。我不知道,也許與片段事情是不同的。

+2

的項目佈局中的「TextView」的ID是'android.R.id.text1',而不是'R.id.text1'。要麼改變你的佈局,要麼改變你的代碼。 –

+0

斑點@MikeM。 –

+1

可能的重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) – TheAnonymous010

回答

2

在您的佈局中,您已將默認ID設置爲TextViewandroid:id="@android:id/text1"

和JAVA文件,你是給R.id.text1

使XML或Java的變化。從android:id="@android:id/text1"android:id="@+id/text1" 或Java中android.R.id.text1

+0

謝謝你正是我所需要的。 – user1591668

+0

我的榮幸..:) – LearnPainLess

0
@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; 
} 

從你提供的XML代碼在XML

醚的變化,我可以看到你沒有爲TextView提供了一個正確的ID。將XMLandroid:id="@android:id/text1"更改爲android:id="@+id/text1"

+0

哪個「上面給出的答案」?您可以更新您的代碼以提供前面提供的答案的正確ID –

+0

@ cricket_007,它已告訴提問用戶,他爲'TextView'添加的ID在他的'findViewById()'中不正確。所以我提到要查看並添加正確的ID。 – W4R10CK

+0

我明白,但引用其他答案本身並不完全提供答案 –

0

在你genre_list_text.xml變化機器人:ID = 「@機器人:ID/text1」 中到Android:ID = 「@ + ID/text1」 中

,並在您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; 
     } 
相關問題