我有一个带按钮的 fragment ,单击该按钮应该会更改我的应用程序中的主题,但是当我打开类(class)时,我只会收到此错误: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)
我的类(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;
}
}
}
我的 xml 文件:
<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="样式三"
/>
请帮我解决这个问题。
最佳答案
您的按钮位于何处?在 Activity 或 fragment 布局中?看起来你正试图得到它两次。一次来自 InitView() 内的 Activity ;然后再次在 onCreateView() 内;
这就是你的问题。如果您的 XML 布局从 Activity 中膨胀,请使用 getActivity().findViewById(R.id.FirstThem);
如果它在 fragment 布局内,则使用 view.findViewById(R.id.FirstThem);
代码示例:
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;
}
}
关于java - 错误 Android 尝试调用虚拟方法 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32017596/