错误Android尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)' [英] Error Android Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'

查看:517
本文介绍了错误Android尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带按钮的片段,单击按钮应会更改应用程序中的主题,但是当我打开课程时,我只会遇到此错误:这是logcat

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)

我的课:

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

请帮助我解决问题.

推荐答案

您的按钮在哪里?在活动中还是在碎片布局中?看来您正在尝试两次.一次来自InitView()内部的活动;然后再次进入onCreateView();

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();

这是您的问题所在.如果您的XML布局因活动而膨胀,请使用getActivity.findViewById(R.id.FirstThem);

There lies your problem. If your XML layout is inflated from the activity use getActivity.findViewById(R.id.FirstThem);

如果其在片段布局内,则使用v.findViewById(R.id.FirstThem);

If its inside the fragment layout, then use the 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; 
    } 

} 

这篇关于错误Android尝试调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆