错误Android尝试调用虚拟方法' void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'

时间:2015-08-14 19:44:15

标签: java android fragment onclicklistener

我有一个带按钮的片段,点击该按钮应该更改我的应用程序中的主题但是当我打开课程时我只是得到这个错误:这是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)

我的班级:

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="样式三"
    />

请帮我解决问题。

2 个答案:

答案 0 :(得分:3)

您的按钮位于何处?在活动或片段布局?看起来你想要两次。一旦从InitView()内部的活动;然后再次进入onCreateView();

你的问题就在于此。如果您的XML布局从活动中膨胀,请使用getActivity.findViewById(R.id.FirstThem);

如果它在片段布局中,则使用v.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 v =inflater.inflate(R.layout.guida_tv,container,false);


    SharePreference(); 

    FirstThemButton =(Button) v.findViewById(R.id.FirstThem);
    SecondThemButton =(Button) v.findViewById(R.id.SecondThem);

    ThirdThemButton =(Button) v.findViewById(R.id.ThirdThem);
    ^^^ This is the culprit! Change the name! 

    InitView();

    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.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; 
    } 

} 

答案 1 :(得分:2)

试试这个。

创建一个 onViewCreated 视图并将 setOnClickListener 放入其中。

示例:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) { 
    buttonName.setOnClickListener { 
        Toast.makeText(activity,"Success.", Toast.LENGTH_SHORT).show()
    }
}