Reputation: 118
I want to show a layout if a certain file does not exist. So how do I hide the layout if the file exists? I want my application to run in the background after the first layout is shown.
File myFile = new File("/LoggedIn.txt");
if(myFile.exists()) {
/* do something */
} else {
setContentView(R.layout.activity_main);
}
I have tried this code alone to see if I could even hide the layout...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layoutLogin = (LinearLayout) findViewById(R.id.activity_main_id);
layoutLogin.setVisibility(View.GONE);
}
But it causes my application to crash when it starts.
activity_main
<LinearLayout android:id="@+id/activity_main_id"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</LinearLayout>
LogCat:
08-12 00:37:29.145 30151-30151/? I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
08-12 00:37:29.145 30151-30151/? I/SELinux﹕ Function: selinux_android_load_priority [1], There is no sepolicy version file.
08-12 00:37:29.145 30151-30151/? I/SELinux﹕ Function: selinux_android_load_priority , priority is 3. priority version is VE=GOOGLE_POLICY
08-12 00:37:29.155 30151-30151/? D/dalvikvm﹕ Late-enabling CheckJNI
08-12 00:37:29.425 30151-30151/? D/ActivityThread﹕ handleBindApplication:com.example.steven.myapplication
08-12 00:37:29.685 30151-30151/? W/ApplicationPackageManager﹕ getCSCPackageItemText()
08-12 00:37:29.705 30151-30151/? D/DisplayManager﹕ DisplayManager()
08-12 00:37:29.835 30151-30151/? D/AndroidRuntime﹕ Shutting down VM
08-12 00:37:29.835 30151-30151/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41d0ad58)
08-12 00:37:29.855 30151-30151/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.steven.myapplication, PID: 30151
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.steven.myapplication/com.example.steven.myapplication.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.steven.myapplication.MainActivity.onCreate(MainActivity.java:105)
at android.app.Activity.performCreate(Activity.java:5442)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
at android.app.ActivityThread.access$800(ActivityThread.java:166)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5584)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Upvotes: 0
Views: 129
Reputation: 143
You can have an empty activity with may be a logo of your app in the middle and check if file exists in this activity. If file exists you can start your service and kill this empty activity else you can start the activity you want to display.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*activity_main can have a logo of your app or
can show progress circle in the middle*/
setContentView(R.layout.activity_main);
File myFile = new File("/LoggedIn.txt");
if(myFile.exists()) {
/* Call service to do something */
finish();
} else {
/* Call another activity using intent */
finish();
}
}
Upvotes: 1