gpt4 book ai didi

java - 我应该在哪里初始化我的开关小部件 Android

转载 作者:行者123 更新时间:2023-12-01 14:20:35 26 4
gpt4 key购买 nike

我的应用程序中有一个选项菜单,其中我有一个选项可以打开一个包含一些小部件的对话框。我无法弄清楚我应该在哪里初始化我的小部件,以便它不会给出空指针异常。
我应该放在哪里 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) 语句时,此错误消失

最佳答案

我认为这会起作用,因为 Switch View 位于对话框内,您应该使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com