[英]Where should I initialize my switch widget Android
我的應用程序中有一個選項菜單,其中有一個選項可以打開一個包含一些小部件的對話框。 我無法弄清楚我應該在哪里初始化我的小部件,以便它不會給出空指針異常。
我應該把Switch sw = findViewById(R.id.switch1)
放在哪里,以便我可以在對話框打開之前使用sw.setChecked(<condition gets decided in the code>)
。
主活動.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) 語句時,此錯誤消失
你應該做這個。
主要活動
package com.example
//imports
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//initialize the switch here
Switch sw = findViewById(R.id.switch1);
if(sharedPreferenceObj == your logic){
//your business logic here
}else{
//if condition is false then do stuff here
}
}
@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;
}
}
我認為這會起作用,因為 Switch 視圖在對話框內,你應該使用 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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.