我一直在試圖建立我的第一個Android應用程序,是的......卡住了一個應用程序崩潰/隱藏可見性......當的Android應用程序崩潰顯示ImageView的
我有兩個ImageViews在我片段(主要),在相對佈局中將imageview 1與imageview2重疊。我想要做的是,如果有Internet連接,imageview1(不是imageview 2)應該顯示出來。反之亦然,如果沒有互聯網連接。作爲每一個教程,我創建一個單獨的類,用於檢測連接:
package com.mypackage.myapp;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
而稱爲在我mainactivity.class類,有由如果用於連接的兩個實例說明(連接/斷開)一個布爾變量......最後,我在onCreate()的if語句中爲每個ImageViews使用了setVisibility()。下面的代碼:
@Override
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);
}
}
此外,如果你想要我的佈局文件:
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/backrepeatlinen"
tools:context="com.mypackage.myapp.MainActivity$PlaceholderFragment" >
<ImageView
android:id="@+id/glass1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/glass" android:contentDescription="@string/hello_world"/>
<ImageView
android:id="@+id/glass2"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/glass1"
android:layout_centerHorizontal="true"
android:contentDescription="@string/hello_world"
android:src="@drawable/broken" />
每次,當我在平板電腦上運行的程序,它崩潰的活動。我試圖刪除連接探測器,如果聲明的事情,活動運行良好。
編輯:唯一的問題是在setVisiblity() if語句,因爲在取出時,一切工作正常。有可能解決嗎?還是我們可以用不同的方法來控制兩個圖像瀏覽的可見性?
的logcat:
05-21 11:05:38.432: E/filePathInTheme(23435): fallback to res
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的了。 – thenextbiggeek