尝试从另一个类实例化方法时,Fragment中的NullPointer异常

时间:2016-07-21 13:59:46

标签: android android-fragments

我正在尝试检查实现ConnectivityReceiver.ConnectivityReceiverListener的片段中的互联网连接。守则如下

ConnectivityReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectivityReceiver extends BroadcastReceiver {

public static ConnectivityReceiverListener connectivityReceiverListener;

public ConnectivityReceiver() {
    super();
}

@Override
public void onReceive(Context context, Intent arg1) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    boolean isConnected = activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();

    if (connectivityReceiverListener != null) {
        connectivityReceiverListener.onNetworkConnectionChanged(isConnected);
    }
}

public static boolean isConnected() {
    ConnectivityManager
            cm = (ConnectivityManager) MyApplication.getInstance().getApplicationContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    return activeNetwork != null
            && activeNetwork.isConnectedOrConnecting();
}


public interface ConnectivityReceiverListener {
    void onNetworkConnectionChanged(boolean isConnected);
}
}

MyApplication.java

import android.app.Application;

public class MyApplication extends Application {

private static MyApplication mInstance;

@Override
public void onCreate() {
    super.onCreate();

    mInstance = this;
}

public static synchronized MyApplication getInstance() {
    return mInstance;
}

public void setConnectivityListener(ConnectivityReceiver.ConnectivityReceiverListener listener) {
    ConnectivityReceiver.connectivityReceiverListener = listener;
}

}

片段活动有代码

@Override
public void onResume() {
    super.onResume();

    // register connection status listener
  MyApplication.getInstance().setConnectivityListener(this);
}

我已经改变了#34;这个" getactivity()并尝试过。它没有产生任何结果。我使用它时的堆栈跟踪如下

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: highski.developers.cflash, PID: 26183
                                                                       java.lang.RuntimeException: Unable to resume activity {highski.developers.cflash/highski.developers.cflash.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void highski.developers.cflash.connection_check.MyApplication.setConnectivityListener(highski.developers.cflash.connection_check.ConnectivityReceiver$ConnectivityReceiverListener)' on a null object reference
                                                                           at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2986)
                                                                           at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:135)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void highski.developers.cflash.connection_check.MyApplication.setConnectivityListener(highski.developers.cflash.connection_check.ConnectivityReceiver$ConnectivityReceiverListener)' on a null object reference
                                                                           at highski.developers.cflash.fragments.HomeFragment.onResume(HomeFragment.java:362)
                                                                           at android.support.v4.app.Fragment.performResume(Fragment.java:2020)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1107)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1234)
                                                                           at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:2056)
                                                                           at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:196)
                                                                           at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:504)
                                                                           at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:493)
                                                                           at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:178)
                                                                           at android.app.Activity.performResume(Activity.java:6089)
                                                                           at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2975)
                                                                           at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3017) 
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:135) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

请让我知道错误是什么以及如何解决。

2 个答案:

答案 0 :(得分:3)

首先检查getInstance方法是否返回null,如果是,则检查Android Manifest。


你的清单应该包含类似的内容:

SELECT  t.Name, 
        p.[Value],
        MIN(t.[Timestamp]) as EarliestTimestamp ,
        MAX(t.[Timestamp]) as LatestTimestamp
FROM Table1 t
OUTER APPLY (SELECT TOP 1 * FROM Table1 WHERE t.Name = Name ORDER BY [Timestamp] DESC) p
GROUP BY t.Name, p.[Value]

答案 1 :(得分:0)

必须是您的应用程序未在清单中声明,添加它。

和两个建议: 首先,connectivityReceiverListener是静态的,你注册它,记得unRegister它,否则它会泄漏内存。 其次,你的get实例是复杂而低效的,不需要使用synchronized方法!只需使用MyApplication.sIntance(声明公开)。