【发布时间】:2014-05-21 13:40:57
【问题描述】:
今天我在我的第一个应用程序上问了一个问题,以找出为什么我的应用程序在测试连接并根据它提供视图时崩溃...答案完全不准确且无用...但是今天晚上我发现没有onCreate 中的代码,但 If 语句中的 setVisibility() 方法导致我的活动崩溃:
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);
}
}
没有这些 setVisibility 方法,整个代码就像一个魅力,但我只想在 if 语句中控制两个 ImageView 的可见性......有解决方案吗?如果不可能,是否有其他方法可以控制 Java 代码中 ImageView 的可见性? 这是日志猫:
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
非常感谢:)
【问题讨论】:
-
请堆栈跟踪(Logcat 输出)
-
我已经更新了我的 logcat
-
发布你在logCat中显示的完整消息,这个“05-20 21:11:05.412: E/filePathInTheme(19298): fallback to re”,没有异常的意义。
-
我最初对如何导出文件感到困惑,现在我更新了我的问题。
标签: android if-statement crash