今天我在我的第一个应用程序上问了一个问题,找出为什么我的应用程序崩溃时测试连接并提供视图...答案是完全不准确和无用的...但今天晚上我发现没有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可见性? 这是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
谢谢。)
答案 0 :(得分:0)
这也是我遇到的问题。似乎Boolean
必须是小写的:boolean
才能正常工作。
答案 1 :(得分:0)
使用boolean
类型的Java原始变量而不是Boolean
对象。
Boolean isInternetPresent = cdr.isConnectingToInternet(); //INCORRECT!
boolean isInternetPresent = cdr.isConnectingToInternet(); //CORRECT =)!
我认为这是很多人的同一问题,PlaceholderFragment()
必须有一个包含
ImageView glss2 = (ImageView)findViewById(R.id.glass2);
ImageView glss1 = (ImageView) findViewById(R.id.glass1);
而不是activity_main.xml
答案 2 :(得分:0)
isInternetPresent可能为null ...或者是imageviews。它们在activity_main布局上是否正确?你确定图像视图的ID是正确的吗?插入logcat消息,它可能对我们有帮助。