Today I asked a question on my first app, to find why my app crashes when testing for connections and providing views according to that... The answers were totally inaccurate and useless... But today evening I found out that none of the code in onCreate, but the setVisibility() method in the If statement caused my activity to crash:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent cd = getIntent();
ConnectionDetector cdr = new ConnectionDetector(getApplicationContext());
Boolean isInternetPresent = cdr.isConnectingToInternet();
ImageView glss2 = (ImageView)findViewById(R.id.glass2);
ImageView glss1 = (ImageView) findViewById(R.id.glass1);
if (isInternetPresent) {
glss1.setVisibility(View.VISIBLE);
glss2.setVisibility(View.GONE);
} else {
glss1.setVisibility(View.GONE);
glss2.setVisibility(View.VISIBLE);
}
}
The whole code works like a charm without these setVisibility methods, but i just want something to control the visibility of two ImageViews in an if statement... Is there a solution? If not possible, are there alternatives to control visibility of an ImageView inside java code? here's the logcat:
05-21 11:05:38.462: E/filePathInTheme(23435): fallback to res
05-21 11:05:38.482: E/filePathInTheme(23435): fallback to res
05-21 11:05:38.652: E/filePathInTheme(23435): fallback to res
05-21 11:05:38.802: I/Adreno200-EGLSUB(23435): <ConfigWindowMatch:2081>: Format RGBA_8888.
05-21 11:05:38.812: D/memalloc(23435): /dev/pmem: Mapped buffer base:0x50c32000 size:11960320 offset:10485760 fd:54
05-21 11:05:39.122: D/memalloc(23435): /dev/pmem: Mapped buffer base:0x51a3b000 size:13434880 offset:11960320 fd:57
05-21 11:05:41.542: E/filePathInTheme(23435): fallback to res
05-21 11:05:41.572: W/dalvikvm(23435): threadid=1: thread exiting with uncaught exception (group=0x40a659f0)
05-21 11:05:41.582: E/AndroidRuntime(23435): FATAL EXCEPTION: main
05-21 11:05:41.582: E/AndroidRuntime(23435): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lemonaade.watchr/com.lemonaade.watchr.MainActivity}: java.lang.NullPointerException
05-21 11:05:41.582: E/AndroidRuntime(23435): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1961)
05-21 11:05:41.582: E/AndroidRuntime(23435): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1986)
05-21 11:05:41.582: E/AndroidRuntime(23435): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-21 11:05:41.582: E/AndroidRuntime(23435): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1152)
05-21 11:05:41.582: E/AndroidRuntime(23435): at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 11:05:41.582: E/AndroidRuntime(23435): at android.os.Looper.loop(Looper.java:137)
05-21 11:05:41.582: E/AndroidRuntime(23435): at android.app.ActivityThread.main(ActivityThread.java:4450)
05-21 11:05:41.582: E/AndroidRuntime(23435): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 11:05:41.582: E/AndroidRuntime(23435): at java.lang.reflect.Method.invoke(Method.java:511)
05-21 11:05:41.582: E/AndroidRuntime(23435): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
05-21 11:05:41.582: E/AndroidRuntime(23435): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
05-21 11:05:41.582: E/AndroidRuntime(23435): at dalvik.system.NativeStart.main(Native Method)
05-21 11:05:41.582: E/AndroidRuntime(23435): Caused by: java.lang.NullPointerException
05-21 11:05:41.582: E/AndroidRuntime(23435): at com.lemonaade.watchr.MainActivity.onCreate(MainActivity.java:35)
05-21 11:05:41.582: E/AndroidRuntime(23435): at android.app.Activity.performCreate(Activity.java:4465)
05-21 11:05:41.582: E/AndroidRuntime(23435): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-21 11:05:41.582: E/AndroidRuntime(23435): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1925)
05-21 11:05:41.582: E/AndroidRuntime(23435): ... 11 more
Ton of thanks :)