我正在尝试创建一个带有登录屏幕的应用程序,该屏幕出现并在其他各种选项卡中产生影响。
我的选项卡式实现工作正常:我可以隐藏选项卡并查看其他选项卡的布局——但是,我不能让它们重新出现。当我不尝试按钮处理时,程序运行良好,但每当我启动我的监听器时,我都会强制关闭。
附件是问题代码:我无法准确找到我从哪里获得力,但我知道它在按钮侦听器区域。
本质上,如果用户名和密码正确,我希望显示选项卡。
您看到的是四个选项卡之一。具体来说,您正在查看登录屏幕选项卡。
代码:
public class LoginActivity extends Activity {
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
final Button btn = (Button) findViewById(R.id.login_button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText txt_username = (EditText) findViewById(R.id.txt_username);
EditText txt_password = (EditText) findViewById(R.id.txt_username);
if (txt_username.toString().equals("Bob")
&& txt_password.toString().equals("123")) {
tabHost.getTabWidget().getChildAt(0)
.setVisibility(View.VISIBLE);
tabHost.getTabWidget().getChildAt(1)
.setVisibility(View.VISIBLE);
tabHost.getTabWidget().getChildAt(2)
.setVisibility(View.VISIBLE);
tabHost.getTabWidget().getChildAt(3)
.setVisibility(View.GONE);
}
}
});
}
}
附上logcat(很长)
04-12 17:07:31.465: ERROR/AndroidRuntime(1480): FATAL EXCEPTION: main
04-12 17:07:31.465: ERROR/AndroidRuntime(1480): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.waiter/android.waiter.waiter}: java.lang.RuntimeException: Unable to start activity ComponentInfo{android.waiter/android.waiter.LoginActivityGroup}: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{android.waiter/android.waiter.LoginActivity}: java.lang.NullPointerException
04-12 17:07:31.465: ERROR/AndroidRuntime(1480): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-12 17:07:31.465: ERROR/AndroidRuntime(1480): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-12 17:07:31.465: ERROR/AndroidRuntime(1480): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-12 17:07:31.465: ERROR/AndroidRuntime(1480): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-12 17:07:31.465: ERROR/AndroidRuntime(1480): at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 17:07:31.465: ERROR/AndroidRuntime(1480): at android.os.Looper.loop(Looper.java:123)
04-12 17:07:31.465: ERROR/AndroidRuntime(1480): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-12 17:07:31.465: ERROR/AndroidRuntime(1480): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 17:07:31.465: ERROR/AndroidRuntime(1480): at java.lang.reflect.Method.invoke(Method.java:521)
04-12 17:07:31.465: ERROR/AndroidRuntime(1480): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-12 17:07:31.465: ERROR/AndroidRuntime(1480): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-12 17:07:31.465: ERROR/AndroidRuntime(1480): at dalvik.system.NativeStart.main(Native Method)