我的应用程序中有一个选项菜单,其中有一个选项可以打开一个包含一些小部件的对话框。我无法弄清楚应该在哪里初始化我的小部件,以便它不会给出空指针异常。
我应该在哪里放置Switch sw = findViewById(R.id.switch1),以便在对话框打开之前可以使用sw.setChecked(<condition gets decided in the code>)
MainActivity.java:

package com.example
//imports
public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {

        switch (item.getItemId())
        {
            case R.id.option:
                sw.setChecked(true);
                Dialog dialog = new Dialog(this);
                dialog.setContentView(R.layout.dialog);
                dialog.show();

            default:
                return false;
        }
    }


我在OnCreate()中调用sw.setChecked时出现错误日志
-04 16:38:48.358 2395 2395 E     AndroidRuntime                               FATAL EXCEPTION: main
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               Process: com.PjMathematician.ImgMath, PID: 2395
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.PjMathematician.ImgMath/com.PjMathematician.ImgMath.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2855)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3093)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at android.os.Handler.dispatchMessage(Handler.java:106)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at android.os.Looper.loop(Looper.java:193)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at android.app.ActivityThread.main(ActivityThread.java:6865)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at java.lang.reflect.Method.invoke(Native Method)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:504)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at android.app.Activity.findViewById(Activity.java:4125)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at com.PjMathematician.ImgMath.MainActivity.<init>(MainActivity.java:307)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at java.lang.Class.newInstance(Native Method)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at android.app.Instrumentation.newActivity(Instrumentation.java:1231)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2842)
09-04 16:38:48.358 2395 2395 E     AndroidRuntime                               ... 11 more
当我注释掉sw.setChecked(true)语句时,此错误消失

最佳答案

我认为这会起作用,因为“切换”视图位于对话框内部,因此应使用dialog.findViewById(R.id.switch1);来获取其实例。

package com.example
//imports
public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {

        switch (item.getItemId())
        {
            case R.id.option:
                sw.setChecked(true);
                Dialog dialog = new Dialog(this);
                dialog.setContentView(R.layout.dialog);
                Switch sw = dialog.findViewById(R.id.switch1);
                sw.setChecked(condition);
                dialog.show();

            default:
                return false;
        }
    }

关于java - 我应该在哪里初始化我的Android开关小部件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63740607/

10-10 22:29