Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd a layout dynamically, to overlapp the other layout
    primarykey
    data
    text
    <p>I'm writing a "Welcome Screen" for my application, that runs when application launches for the first time. <strong>What I want:</strong> A transparent layout to be overlapped on the root layout, dynamically through code. like this image:</p> <p><img src="https://i.stack.imgur.com/3mhjN.png" alt="enter image description here"></p> <p>but when I run my app, It crashes due to NULLPOINTEREXCEPTION. even when I didn't add the second layout</p> <p><strong>Problem 1:</strong> Why first layout doesn't show up, and app crashes?</p> <p><strong>Problem 2:</strong> How to show the second layout?</p> <p>thanks in advance....</p> <p><strong>Authentication.java</strong></p> <pre><code>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 } } } </code></pre> <p><strong>authentication.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;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" &gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;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" /&gt; &lt;/LinearLayout&gt; </code></pre> <p><strong>stack trace:</strong></p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload