2016-09-15 12 views
-1

私にはクラスがあり、これには接続マネージャを使用するメソッドがあります。このクラスのこのメソッドは、接続性をチェックするためにフラグメントによって呼び出されます。しかし、これを行うと、NULLポインタ例外が発生します。getsystemサービスがクラスからフラグメントに呼び出されたときのヌルポインタ例外

私は接続マネージャーのコンテキストとしてgetActivity()を使用しようとしましたが、それはまったく助けませんでした。ここでは、コードは次のようになります。
クラスファイル:

public class Common_Tasks implements AsyncResponse { 
int InternetConnectionStatus=-1; 
static int MemberShipStatus=-1; 

Context ctx; 
Common_Tasks(Context context) 
{ 
    this.ctx = context; 
} 

public void ShowMessage(String msg) 
{ 
    Toast.makeText(ctx,msg,Toast.LENGTH_SHORT).show(); 
} 


public int CheckInternetConnectivity(){ 
    ConnectivityManager connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); 
    if(networkInfo!=null && networkInfo.isConnected()){ 
     InternetConnectionStatus=1; 
    } 
    else { 
     InternetConnectionStatus=0; 
    } 
    return InternetConnectionStatus; 
} 

public void StartAsyncActivity() 
{ 
    BackgroundTask backgroundTask = new BackgroundTask(); 
    backgroundTask.delegate = this; 
    backgroundTask.execute(); 
} 

@Override 
public void processFinish(String output) { 
    if(output.equals("true")) 
     MemberShipStatus=1; 
    else if(output.equals("false")) 
     MemberShipStatus=0; 
} 
} 

フラグメント:

public class NoInternetConnection extends Fragment { 
Common_Tasks commonTasks = new Common_Tasks(getActivity()); 

public NoInternetConnection() { 

} 


@Override 
public View onCreateView(LayoutInflater inflater, final ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_nointernet, container, false); 

    final SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_id); 
    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
     @Override 
     public void onRefresh() { 
      swipeRefreshLayout.setRefreshing(true); 
      Handler handler = new Handler(Looper.getMainLooper()); 
      Runnable r = new Runnable() { 
       @Override 
       public void run() { 
        swipeRefreshLayout.setRefreshing(false); 
        if(commonTasks.CheckInternetConnectivity()==1) 
         commonTasks.ShowMessage("Internet connection restored!"); 
        else 
         commonTasks.ShowMessage("No Internet connection"); 
       } 
      }; 
      handler.postDelayed(r, 1500); 
     } 
    }); 

    return view; 
} 

} 

エラーログ:

09-15 17:09:18.209 16400-16400/com.example.tonymathew.eliteenglishclub E/AndroidRuntime: FATAL EXCEPTION: main 
                         Process: com.example.tonymathew.eliteenglishclub, PID: 16400 
                         java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference 
                          at com.example.tonymathew.eliteenglishclub.Common_Tasks.CheckInternetConnectivity(Common_Tasks.java:25) 
                          at com.example.tonymathew.eliteenglishclub.NoInternetConnection$1$1.run(NoInternetConnection.java:37) 
                          at android.os.Handler.handleCallback(Handler.java:746) 
                          at android.os.Handler.dispatchMessage(Handler.java:95) 
                          at android.os.Looper.loop(Looper.java:148) 
                          at android.app.ActivityThread.main(ActivityThread.java:5443) 
                          at java.lang.reflect.Method.invoke(Native Method) 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
09-15 17:14:11.634 19927-19927/com.example.tonymathew.eliteenglishclub E/AndroidRuntime: FATAL EXCEPTION: main 
                         Process: com.example.tonymathew.eliteenglishclub, PID: 19927 
                         java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference 
                          at com.example.tonymathew.eliteenglishclub.Common_Tasks.CheckInternetConnectivity(Common_Tasks.java:25) 
                          at com.example.tonymathew.eliteenglishclub.NoInternetConnection$1$1.run(NoInternetConnection.java:37) 
                          at android.os.Handler.handleCallback(Handler.java:746) 
                          at android.os.Handler.dispatchMessage(Handler.java:95) 
                          at android.os.Looper.loop(Looper.java:148) 
                          at android.app.ActivityThread.main(ActivityThread.java:5443) 
                          at java.lang.reflect.Method.invoke(Native Method) 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 

答えて

0

あなたはアンドロイドのライフサイクルを尊重しなければなりません。これはアンドロイドの初心者のための共通の問題です。あなたはこの時点でFragmentので、ここで

public class NoInternetConnection extends Fragment { 
Common_Tasks commonTasks = new Common_Tasks(getActivity()); 

これを置くことができない Activityに接続されていません。それをFragmentonCreate()メソッドに入れてください。今後の参考資料については、android lifecycle

+0

を参照してください。ありがとうございました。 –

0
public class NoInternetConnection extends Fragment { 
    Common_Tasks commonTasks; 

    public NoInternetConnection() { 

    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, 
          Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_nointernet, container, false); 
     commonTasks = new Common_Tasks(getActivity()); 

} 
関連する問題