检查活动是否从非活动类运行

时间:2013-01-05 14:02:24

标签: android class android-activity

我想检查活动是否从常规类(非活动)运行但应用程序崩溃并给我NullPointerException并且我确实设置了permission GET_TASKS 这是LogCat输出

    01-05 13:38:38.703: E/AndroidRuntime(5595): FATAL EXCEPTION: main
01-05 13:38:38.703: E/AndroidRuntime(5595): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.evolution.betting.application/net.evolution.betting.application.InitActivity}: java.lang.NullPointerException
01-05 13:38:38.703: E/AndroidRuntime(5595):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-05 13:38:38.703: E/AndroidRuntime(5595):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-05 13:38:38.703: E/AndroidRuntime(5595):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-05 13:38:38.703: E/AndroidRuntime(5595):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-05 13:38:38.703: E/AndroidRuntime(5595):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 13:38:38.703: E/AndroidRuntime(5595):     at android.os.Looper.loop(Looper.java:123)
01-05 13:38:38.703: E/AndroidRuntime(5595):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-05 13:38:38.703: E/AndroidRuntime(5595):     at java.lang.reflect.Method.invokeNative(Native Method)
01-05 13:38:38.703: E/AndroidRuntime(5595):     at java.lang.reflect.Method.invoke(Method.java:521)
01-05 13:38:38.703: E/AndroidRuntime(5595):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-05 13:38:38.703: E/AndroidRuntime(5595):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-05 13:38:38.703: E/AndroidRuntime(5595):     at dalvik.system.NativeStart.main(Native Method)
01-05 13:38:38.703: E/AndroidRuntime(5595): Caused by: java.lang.NullPointerException
01-05 13:38:38.703: E/AndroidRuntime(5595):     at net.evolution.betting.application.ConnectionChangeReceiver.checkIsNotationActive(ConnectionChangeReceiver.java:58)
01-05 13:38:38.703: E/AndroidRuntime(5595):     at net.evolution.betting.application.ConnectionChangeReceiver.<init>(ConnectionChangeReceiver.java:20)
01-05 13:38:38.703: E/AndroidRuntime(5595):     at net.evolution.betting.application.InitActivity.onCreate(InitActivity.java:30)
01-05 13:38:38.703: E/AndroidRuntime(5595):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-05 13:38:38.703: E/AndroidRuntime(5595):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

这是我的来源

public class ConnectionChangeReceiver extends BroadcastReceiver
{
    Context c;
    public ConnectionChangeReceiver(Context c){
        checkIsNotationActive();
        this.c = c;
    }

  @Override
  public void onReceive( Context context, Intent intent )
  {

    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(     ConnectivityManager.TYPE_MOBILE );
    if ( activeNetInfo != null && checkIsNotationActive() == true)
    {

        ((NotationActivity)(context)).finish();

    }else if( activeNetInfo == null){

        Intent i = new Intent("net.evolution.betting.application.NOTATIONACTIVITY");
        i.putExtra("DialogTitle", "Warining!");
        i.putExtra("DialogBody", "It seems like your app is not connected to Internet witch is required.Please check your network connection.");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


        context.startActivity(i);

    }


  }

  private boolean checkIsNotationActive(){


          ActivityManager activityManager = (ActivityManager)c.getSystemService (Context.ACTIVITY_SERVICE); 
              List<RunningTaskInfo> activitys = activityManager.getRunningTasks(Integer.MAX_VALUE); 
              boolean isActivityFound = false; 
              for (int i = 0; i < activitys.size(); i++) { 
                  if (activitys.get(i).topActivity.toString().equalsIgnoreCase("ComponentInfo{net.evolution.betting.application/net.evolution.betting.application.NotationActivity}")) {
                      isActivityFound = true;
                  }
              } 
              return isActivityFound; 

  }
}

2 个答案:

答案 0 :(得分:2)

更改您的代码,因为您的c上下文实例null将您的代码移到onReceive内并使用上下文访问系统服务:

public class ConnectionChangeReceiver extends BroadcastReceiver
{
    Context c;

  @Override
  public void onReceive( Context context, Intent intent )
  {
     this.c = context;  //<<<<<<<  

     // your code here
  }

}

答案 1 :(得分:0)

抱歉,我无法找到问题所在,但也许您可以尝试以下方法: 当您使用广播接收器时,为什么不尝试在“NotationActivity”的“onCreate()”中发送广播(A),在“onDesdroy()”中发送另一个广播(B)。 然后,您可以通过检查变量来检查活动是否正在运行,可能是一个初始化为0的整数,当收到A时更改为1,而当收到B时更改为2.