android:findViewById在从另一个类访问时返回NULL
我有一个MainActivity.class,我在它上设置ContentView,下面是我使用的三个变量android:findViewById在从另一个类访问时返回NULL,android,findviewbyid,Android,Findviewbyid,我有一个MainActivity.class,我在它上设置ContentView,下面是我使用的三个变量 EditText labelText = (EditText) findViewById(R.id.label_field); EditText phoneText = (EditText) findViewById(R.id.phone_field); Spinner deviceTypeSpinner = (Spinner) findViewById(R.id.d
EditText labelText = (EditText) findViewById(R.id.label_field);
EditText phoneText = (EditText) findViewById(R.id.phone_field);
Spinner deviceTypeSpinner = (Spinner) findViewById(R.id.device_type_spinner);
Button registerButton = (Button) findViewById(R.id.register_button)
现在,我有了一个扩展异步任务的独立类,我正在尝试从异步任务(位于另一个文件中的类)修改上述字段
为了做到这一点,我将活动的上下文传递给异步任务,并执行以下步骤
MainActivity activity = (MainActivity)context;
Button buttonRegister =(Button)activity.findViewById(R.id.register_button);
EditText labelText = (EditText)activity.findViewById(R.id.label_field);
buttonRegister.setEnabled(true);
labelText.setFocusable(true);
但是buttonRegister返回null,应用程序崩溃,有人能告诉我哪里出了问题吗
java.lang.NullPointerException
at com.yantranet.minixagent.requests.DeviceCreateRequest$1.onClick(DeviceCreateRequest.java:237)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
但是buttonRegister返回空值,应用程序崩溃,有人知道吗
告诉我哪里出了问题
java.lang.NullPointerException
at com.yantranet.minixagent.requests.DeviceCreateRequest$1.onClick(DeviceCreateRequest.java:237)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
我看不出你在哪儿打电话
setContentView(<layout>);
由于您在另一个类中调用它,所以它不会起作用(该类对您在另一个类中所做的事情一无所知)。您需要:
- 通过一些构造函数传递小部件
- 更改应用程序逻辑(很可能未正确指定)
java.lang.NullPointerException
at com.yantranet.minixagent.requests.DeviceCreateRequest$1.onClick(DeviceCreateRequest.java:237)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4895)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
at dalvik.system.NativeStart.main(Native Method)
我看不出你在哪儿打电话
setContentView(<layout>);
由于您在另一个类中调用它,所以它不会起作用(该类对您在另一个类中所做的事情一无所知)。您需要:
- 通过一些构造函数传递小部件
- 更改应用程序逻辑(很可能未正确指定)
如果您正在传递正确的活动上下文,那么它应该可以工作,但我不希望将上下文传递给另一个类,因为它与活动无关。您可以使用以下设计
不能在doInBackgroud()内修改布局元素
因此,您可以在这里访问布局参数。您不能在doInBackgroud()中修改布局元素
因此,在这里您可以访问布局参数。这不是一个好方法,但是如果您想访问其他活动的小部件,那么您应该为其创建getter setter
//In you mainActivity
public Button getRegisterButton()
{
//Return register button here.
return btn;
}
public EditText getFieldEditText()
{
//Return editText here;
return eText;
}
在需要此视图的位置编写代码
//在当前的活动中
MainActivity activity = (MainActivity)context;
Button buttonRegister =activity.getRegisterButton();
EditText labelText = activity.getFieldEditText();
if(buttonRegister != null)
{
buttonRegister.setEnabled(true);
}
if(labelText != null)
{
labelText.setFocusable(true);
}
希望这会有所帮助。这不是一个好方法,但是如果你想访问其他活动的小部件,那么你应该使用getter setter
//In you mainActivity
public Button getRegisterButton()
{
//Return register button here.
return btn;
}
public EditText getFieldEditText()
{
//Return editText here;
return eText;
}
在需要此视图的位置编写代码
//在当前的活动中
MainActivity activity = (MainActivity)context;
Button buttonRegister =activity.getRegisterButton();
EditText labelText = activity.getFieldEditText();
if(buttonRegister != null)
{
buttonRegister.setEnabled(true);
}
if(labelText != null)
{
labelText.setFocusable(true);
}
希望这会有所帮助。asynctask类在main类中还是不同?是否将活动上下文传递给asynctask构造函数?@Stacks28:它在另一个类中class@Raghunandan:是,我正在传递活动上下文。我已经试着调试它,并且正在传递上下文。@Code\u发布堆栈跟踪。您是否也在doInbackground中更新ui?asynctask类是否在main类中?是否将活动上下文传递给asynctask构造函数?@Stacks28:它在另一个类中class@Raghunandan:是,我正在传递活动上下文。我已经试着调试它,并且正在传递上下文。@Code\u发布堆栈跟踪。你还在doInbackground中更新ui吗?我已经在主活动中设置了内容视图,我是否必须从同步任务中再次设置内容视图以访问字段?你应该通过构造函数传递小部件,或者将类设置为内部类。我已经像你说的那样从异步任务中再次设置了内容视图,现在一切正常。非常感谢:)我已经在主活动中设置了内容视图,我必须从同步任务中再次设置内容视图才能访问字段吗?你应该通过构造函数传递小部件,或者将类设置为内部类。我已经像你说的那样从异步任务中再次设置了内容视图,现在工作正常了。非常感谢:)实际上,我正在从异步任务的onPostExecute调用一个警报对话框,并且在该对话框中,我正在尝试更新活动GUI,请发布完整的代码,我想看看您要传递给异步任务的上下文是什么,以及您用来更新UI的上下文是什么。实际上,我正在从异步任务的onPostExecute调用一个警报对话框,并且在该对话框中,我正在尝试更新活动GUI。请发布完整的代码,我想看看您传递给异步任务的上下文是什么,以及您用来更新UI的上下文是什么。是的,我是从OnPostExecuteEyes执行的,我是从onPostExecute执行的