我正在为我的应用程序编写一个“欢迎屏幕”,它在应用程序首次启动时运行。 我想要的:通过代码动态地在根布局上重叠的透明布局。像这张图片:
但是当我运行我的应用程序时,它由于 NULLPOINTEREXCEPTION 而崩溃。即使我没有添加第二个布局
问题 1:为什么第一个布局不显示,应用程序崩溃?
问题2:如何显示第二个布局?
提前致谢....
身份验证.java
import component.Util;
public class Authentication extends Activity {
private RelativeLayout rootLayout;
public static Context CONTEXT;
public Authentication(){
CONTEXT=this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootLayout=new RelativeLayout(CONTEXT);
LinearLayout firstLayout=(LinearLayout) findViewById(R.layout.authentication);
rootLayout.addView(firstLayout);
setContentView(rootLayout);
if(Util.isFirstLaunch(CONTEXT)){
//Add the second layout
}
}
}
身份验证.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/authentication"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/background" >
<TextView
android:id="@+id/textView_login"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="12dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_gravity="center_horizontal"
android:text="@string/loginText"
android:textSize="18sp" />
<EditText
android:id="@+id/editText_username"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_gravity="center_horizontal"
android:nextFocusDown="@+id/editText_password"
android:inputType="textPersonName"
android:hint="@string/username" />
<EditText
android:id="@+id/editText_password"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="12dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_gravity="center_horizontal"
android:inputType="textPassword"
android:hint="@string/password" />
<Button
android:id="@+id/button_submit"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/button"
android:text="@string/submit"
android:textColor="#e1ecff" />
</LinearLayout>
堆栈跟踪:
08-31 13:50:32.623: D/AndroidRuntime(5740): Shutting down VM
08-31 13:50:32.623: W/dalvikvm(5740): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-31 13:50:32.643: E/AndroidRuntime(5740): FATAL EXCEPTION: main
08-31 13:50:32.643: E/AndroidRuntime(5740): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shayan.filesharing/com.shayan.filesharing.Authentication}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.RelativeLayout
08-31 13:50:32.643: E/AndroidRuntime(5740): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-31 13:50:32.643: E/AndroidRuntime(5740): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-31 13:50:32.643: E/AndroidRuntime(5740): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-31 13:50:32.643: E/AndroidRuntime(5740): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-31 13:50:32.643: E/AndroidRuntime(5740): at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 13:50:32.643: E/AndroidRuntime(5740): at android.os.Looper.loop(Looper.java:137)
08-31 13:50:32.643: E/AndroidRuntime(5740): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-31 13:50:32.643: E/AndroidRuntime(5740): at java.lang.reflect.Method.invokeNative(Native Method)
08-31 13:50:32.643: E/AndroidRuntime(5740): at java.lang.reflect.Method.invoke(Method.java:511)
08-31 13:50:32.643: E/AndroidRuntime(5740): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-31 13:50:32.643: E/AndroidRuntime(5740): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-31 13:50:32.643: E/AndroidRuntime(5740): at dalvik.system.NativeStart.main(Native Method)
08-31 13:50:32.643: E/AndroidRuntime(5740): Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.RelativeLayout
08-31 13:50:32.643: E/AndroidRuntime(5740): at com.shayan.filesharing.Authentication.onCreate(Authentication.java:22)
08-31 13:50:32.643: E/AndroidRuntime(5740): at android.app.Activity.performCreate(Activity.java:5104)
08-31 13:50:32.643: E/AndroidRuntime(5740): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-31 13:50:32.643: E/AndroidRuntime(5740): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-31 13:50:32.643: E/AndroidRuntime(5740): ... 11 more