ImageView的Andr​​oid的应用程序崩溃时显示/隐藏可视性可视性、应用程序、ImageView、Andr

2023-09-04 07:22:59 作者:浅瞳

我一直在试图建立我的第一个Android应用程序,是的......卡住了一个应用程序崩溃...

I've been trying to create my first android app, and yes... got stuck up with an app crash...

我有两个ImageViews在我的片段(主),在一个相对布局重叠的ImageView 1与imageview2。我想要做的是, imageview1(不ImageView的2)应显示出来,如果​​有一个互联网连接。反之,如果没有互联网连接的作为每一个教程,我创建了一个单独的类用于检测连接:

I had two ImageViews in my fragment (main), overlapping imageview 1 with imageview2 in a relative layout. What I wanted to do was, imageview1 (not imageview 2) should be showing up if there's an Internet connection. and vice versa if no internet connection. As per a tutorial, i created a separate class for detecting connection:

   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,取得了一个if语句连接(连接/断开)与布尔变量的两个实例。最后,我用setVisibility()每个ImageViews的if语句中的onCreate()。这里的code:

And called up the class in my mainactivity.class, made an if statement for two instances of connection (connected/disconnected) with a boolean variable... finally, i used setVisibility() for each ImageViews in the if statement in onCreate(). here's the code:

@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);
    }
}

此外,如果你想要我的布局文件

Additionally, if you want my layout file:

<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" />

每次,当我在我的平板电脑运行的程序,它崩溃的活动。我试图消除连接器和if语句的事情,活动以及移动

Everytime, when I run the program in my tablet, it crashes at the activity. i tried removing the connection detector and if statement thing, the activity moves well.

编辑:唯一的问题是对的 setVisiblity()的if语句,因为在取出时,一切工作正常。是否可以解决?或者我们可以CONTROLL两个imageviews的可视性用不同的方法?

the only problem is on the setVisiblity() in the if statement, because when removing it, everything else works properly. Is it possible to solve? or can we controll the visiblity of two imageviews with a different method?.

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

感谢。

推荐答案

请在下面移动的setContentView这code()。

ImageView glss1 = (ImageView) findViewById(R.id.glass1);
ImageView glss2 = (ImageView)findViewById(R.id.glass2);

您发现视图,然后再设置视图的内容。这就是为什么它不工作。

You are finding view before you set the content of the view. thats why it is not working.