切换按钮时Android应用程序崩溃按

时间:2016-12-09 01:42:37

标签: android xml

我正在尝试制作一个简单的切换按钮,只显示文字。但是,当我按下按钮时,我的应用程序崩溃并显示错误消息"应用已停止"。我认为这与片段和活动没有正确链接有关。这是带有按钮的页面的xml:

<ToggleButton
    android:text="ToggleButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="62dp"
    android:id="@+id/toggleButton"
    android:textOn="Night Mode"
    android:textOff="Day Mode"
    android:layout_below="@+id/textView4"
    android:layout_toLeftOf="@+id/textView4"
    android:layout_toStartOf="@+id/textView4"
    android:layout_marginRight="8dp"
    android:layout_marginEnd="8dp"
    android:onClick="changeTheme"/>

在MainActivity.java中,我定义了函数:

public void changeTheme(View view){
    boolean checked = ((ToggleButton)view).isChecked();

    if(checked)
    {
        textView.setText("On");
        textView.setVisibility(View.VISIBLE);
    }
    else
    {
        textView.setText("Off");
        textView.setVisibility(View.VISIBLE);
    }

}

引用在此处初始化的TextView:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

//this text view
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //initialized the text view here
    textView = (TextView)findViewById(R.id.themeBtnText);

    //initialize view to my events
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.content_frame, new EventsFragment())
            .commit();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

有没有人知道为什么按此按钮会导致应用程序崩溃?如果需要,会发布更多信息。

编辑:以下是Android Studio的完整错误消息

     --------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.thememeteam.eventspoll, PID: 2300
                  java.lang.IllegalStateException: Could not execute method for android:onClick
                      at android.view.View$DeclaredOnClickListener.onClick(View.java:4452)
                      at android.view.View.performClick(View.java:5198)
                      at android.widget.CompoundButton.performClick(CompoundButton.java:122)
                      at android.view.View$PerformClick.run(View.java:21147)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Method.invoke(Native Method)
                      at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
                      at android.view.View.performClick(View.java:5198) 
                      at android.widget.CompoundButton.performClick(CompoundButton.java:122) 
                      at android.view.View$PerformClick.run(View.java:21147) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                      at com.thememeteam.eventspoll.MainActivity.changeTheme(MainActivity.java:111)
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at android.view.View$DeclaredOnClickListener.onClick(View.java:4447) 
                      at android.view.View.performClick(View.java:5198) 
                      at android.widget.CompoundButton.performClick(CompoundButton.java:122) 
                      at android.view.View$PerformClick.run(View.java:21147) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

1 个答案:

答案 0 :(得分:0)

编辑的答案,它有效:

您需要将对象逻辑移动到片段类中。

在您的MainActivity中,您需要删除我已注释掉的此方法:

//     public void changeTheme(View view){
//        boolean checked = ((ToggleButton)view).isChecked();
//
//        if(checked)
//        {
//            textView.setText("On");
//            textView.setVisibility(View.VISIBLE);
//        }
//        else
//        {
//            textView.setText("Off");
//            textView.setVisibility(View.INVISIBLE);
//        }
//
//    }

然后在您的SettingsFragment中,我引用并初始化了TextView和ToggleButton。在ToggleButton中添加了一个onClickListener来处理点击,而不是通过XML进行处理,然后修复了ToggleButton上的布尔检查。

public class SettingsFragment extends Fragment {

View myView;

//added
ToggleButton toggleButton;
TextView textView;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.settings, container, false);

    //added
    textView = (TextView) myView.findViewById(R.id.themeBtnText);
    toggleButton = (ToggleButton) myView.findViewById(R.id.toggleButton);



    toggleButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            changeTheme(myView);
        }
    });







    return myView;
}

public void changeTheme(View view){
    boolean checked = (toggleButton.isChecked());

    if(checked)
    {
        textView.setText("On");
        textView.setVisibility(View.VISIBLE);
    }
    else
    {
        textView.setText("Off");
        textView.setVisibility(View.INVISIBLE);
    }

}

}