【问题标题】:layout is not showing when broadcast receiver is sending message广播接收器发送消息时未显示布局
【发布时间】:2021-09-04 16:08:35
【问题描述】:

我有一个仪表板活动,它有 3 个片段,最初它调用一个主片段,我想在收到 Internet 未连接的广播但我的布局始终显示空指针异常时显示布局...起初,互联网布局的可见性消失了。

当我设置我的可见性时,它会显示并重叠我的片段设计,但是一旦设置它的可见性消失然后想要显示它总是显示错误

为什么会这样

checkNet.java


 DashBoardActivity dashBoardActivity = new DashBoardActivity();

    public void onReceive(Context context, Intent intent) {
        try {
            if (isOnline(context)) {
               dashBoardActivity.checkNet(true);
                Log.e("checkInternet", "App is back to Online  ");

            } else {

                dashBoardActivity.checkNet(false);
                Log.e("checkInternet", "Conectivity Failure  !!! ");
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }


    private boolean isOnline(Context context) {

        try {
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            
            return (netInfo != null && netInfo.isConnected());
        } catch (NullPointerException e) {
            e.printStackTrace();
            return false;
        }

    }
}



此功能在仪表板活动中


   public  void checkNet(boolean value) {

        if (value) {
            Log.e("Connected", "Yes ");
             
        }
        else {
            Log.e("Connected", "NO ");
            something_wrong_LL.setVisibility(View.VISIBLE);
           
}

           


dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="DashBoardActivity">





            <LinearLayout
                android:id="@+id/something_wrong_LL"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:gravity="center"
                android:visibility="gone"
                app:layout_constraintBottom_toTopOf="@+id/card1"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" >


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/nunito"
                    android:text="somethingWrong"
                    android:textColor="@color/gray_2"/>

                <Button
                    android:id="@+id/retry_BT"
                    android:layout_marginTop="20dp"
                    android:layout_width="wrap_content"
                    android:layout_height="35dp"
                    android:text="RETRY"
                    android:background="@drawable/blue_background_bg"
                    android:textColor="@color/whiteColor"/>

            </LinearLayout>






    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="visible"
        app:layout_constraintBottom_toTopOf="@+id/card1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>


    <com.google.android.material.card.MaterialCardView
        android:id="@+id/card1"
        android:layout_width="0dp"
        app:contentPadding="0dp"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentBottom="true"
        android:theme="@style/Theme.MaterialComponents.Light.NoActionBar"
        app:cardElevation="16dp"
        app:cardPreventCornerOverlap="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">



       .
       .
       .
       .
       .
       .
       .
        
    </com.google.android.material.card.MaterialCardView>


</androidx.constraintlayout.widget.ConstraintLayout>




【问题讨论】:

  • 能分享一下空指针异常堆栈跟踪吗?此外,您提供的布局格式不正确,因为它包含点。
  • W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference .............@tomerpacific 这是错误,点是底部导航..如果你愿意,我也可以上传
  • 这个异常是从代码的什么地方产生的?请添加代码。

标签: java android android-fragments broadcastreceiver


【解决方案1】:

https://developer.android.com/guide/components/fundamentals#ActivatingComponents

BroadcastReceiver 和 Activity 都是应用组件 因此,最好将它们激活为Intent

Activity从onReceive()内部调用到startActivity()

public void onReceive(Context context, Intent intent) {
        try {
            Intent activityIntent = new Intent(context, TestActivity.class);
            boolean isOnlineState = isOnline(context);
            
            // to execute checkNet()
            intent.putExtra("checkInternet", isOnlineState);

            if (isOnlineState) {
                Log.e("checkInternet", "App is back to Online  ");
            } else {
                Log.e("checkInternet", "Conectivity Failure  !!! ");
            }

            context.startActivity(activityIntent);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

【讨论】:

  • 你能详细说明一下吗
  • 添加代码sn-p,你可以添加intent extra来使用checknet()参数
猜你喜欢
  • 1970-01-01
  • 2012-06-05
  • 2011-04-23
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 2018-02-27
  • 2021-12-18
  • 1970-01-01
相关资源
最近更新 更多