Error Android Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
10,276
Solution 1
Where is your button located? In the activity or the fragments layout? Looks like you are trying to get it twice. Once from the activity inside InitView(); and then again inside onCreateView();
There lies your problem. If your XML layout is inflated from the activity use getActivity().findViewById(R.id.FirstThem);
If its inside the fragment layout, then use the view.findViewById(R.id.FirstThem);
Code Example:
public class GuidaTv extends Fragment implements View.OnClickListener{
private Button FirstThemButton;
private Button SecondThemButton;
private Button ThirdThemButton;
private SharedPreferences sharePrefences;
private SharedPreferences.Editor editor;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.guida_tv,container,false);
SharePreference();
FirstThemButton =(Button) view.findViewById(R.id.FirstThem);
SecondThemButton =(Button) view.findViewById(R.id.SecondThem);
ThirdThemButton =(Button) view.findViewById(R.id.ThirdThem);
^^^ This is the culprit! Change the name!
InitView();
return view;
}
private void SharePreference() {
sharePrefences=this.getActivity().getSharedPreferences("config", Context.MODE_WORLD_READABLE
| Context.MODE_WORLD_WRITEABLE);
editor=sharePrefences.edit();
boolean isThem = sharePrefences.getBoolean("isThem", false);
int Them = sharePrefences.getInt("Them",0);//config不存在时返回0
if(isThem){
if(Them==1){
getActivity().setTheme(R.style.AppTheme2);
}else if(Them==2){
getActivity().setTheme(R.style.AppTheme2);
}else if(Them==3){
getActivity().setTheme(R.style.AppTheme2);
}
}else{//sharePrefences不存在是使用默认主题
getActivity().setTheme(R.style.AppTheme);
}
}
private void InitView() {
FirstThemButton.setOnClickListener(this);
SecondThemButton.setOnClickListener(this);
ThirdThemButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.FirstThem:
editor.putBoolean("isThem", true);
editor.putInt("Them", 1);
editor.commit();
break;
case R.id.SecondThem:
editor.putBoolean("isThem", true);
editor.putInt("Them",2);
editor.commit();
break;
case R.id.ThirdThem:
editor.putBoolean("isThem", true);
editor.putInt("Them", 3);
editor.commit();
break;
default:
break;
}
}
Solution 2
Try this.
create one onViewCreated view and place the setOnClickListener inside it.
example:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
buttonName.setOnClickListener {
Toast.makeText(activity,"Success.", Toast.LENGTH_SHORT).show()
}
}
Author by
Simone Varletta
Updated on June 18, 2022Comments
-
Simone Varletta almost 2 years
I have a fragment with button, on click the button should change the theme in my app but when i open the class i simply get this error: This is the
logcat
08-14 21:32:45.914 29624-29624/addid E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: appid, PID: 29624 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at appid.Fragment.GuidaTv.InitView(GuidaTv.java:75) at appid.Fragment.GuidaTv.onCreateView(GuidaTv.java:41) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501) at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) 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:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
My Class:
public class GuidaTv extends Fragment implements View.OnClickListener{ private Button FirstThemButton; private Button SecondThemButton; private Button ThirdThemButton; private SharedPreferences sharePrefences; private SharedPreferences.Editor editor; @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v =inflater.inflate(R.layout.guida_tv,container,false); SharePreference(); InitView(); FirstThemButton =(Button) v.findViewById(R.id.FirstThem); SecondThemButton =(Button) v.findViewById(R.id.SecondThem); SecondThemButton =(Button) v.findViewById(R.id.ThirdThem); return v; } private void SharePreference() { sharePrefences=this.getActivity().getSharedPreferences("config", Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE); editor=sharePrefences.edit(); boolean isThem = sharePrefences.getBoolean("isThem", false); int Them = sharePrefences.getInt("Them",0);//config不存在时返回0 if(isThem){ if(Them==1){ getActivity().setTheme(R.style.AppTheme2); }else if(Them==2){ getActivity().setTheme(R.style.AppTheme2); }else if(Them==3){ getActivity().setTheme(R.style.AppTheme2); } }else{//sharePrefences不存在是使用默认主题 getActivity().setTheme(R.style.AppTheme); } } private void InitView() { FirstThemButton=(Button) getActivity().findViewById(R.id.FirstThem); SecondThemButton=(Button) getActivity().findViewById(R.id.SecondThem); ThirdThemButton=(Button) getActivity().findViewById(R.id.ThirdThem); FirstThemButton.setOnClickListener(this); SecondThemButton.setOnClickListener(this); ThirdThemButton.setOnClickListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.FirstThem: editor.putBoolean("isThem", true); editor.putInt("Them", 1); editor.commit(); break; case R.id.SecondThem: editor.putBoolean("isThem", true); editor.putInt("Them",2); editor.commit(); break; case R.id.ThirdThem: editor.putBoolean("isThem", true); editor.putInt("Them", 3); editor.commit(); break; default: break; } } }
my xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="#E5E5E5" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/FirstThem" android:layout_width="10dp" android:layout_height="10dp" android:text="样式一" /> <Button android:id="@+id/SecondThem" android:layout_width="10dp" android:layout_height="10dp" android:text="样式二" /> <Button android:id="@+id/ThirdThem" android:layout_width="10dp" android:layout_height="10dp" android:text="样式三" />
Please help me to resolve the problem.