我一直在构建在线应用程序,我的主要功能之一是检查它是否已连接到Internet。我决定用Snackbar
来表明这一点。但不幸的是,我未能做到这一点。我的应用程序在我的手机上运行时崩溃,它指向java.lang.NullPointerException
。我不知道我在这里遗漏了什么,或者我的编码有错。谁能帮助我?谢谢!
显示java.lang.NullPointerException
01-12 17:09:35.273 32723-32723/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.cvsuimus.theflare, PID: 32723
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.cvsuimus.theflare/org.cvsuimus.theflare.MainPageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context org.cvsuimus.theflare.MyApplication.getApplicationContext()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5631)
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:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context org.cvsuimus.theflare.MyApplication.getApplicationContext()' on a null object reference
at org.cvsuimus.theflare.ConnectivityReceiver.isConnected(ConnectivityReceiver.java:33)
at org.cvsuimus.theflare.RecentNewsFragment.loadNews(RecentNewsFragment.java:70)
at org.cvsuimus.theflare.RecentNewsFragment.onCreateView(RecentNewsFragment.java:50)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1244)
at android.app.Activity.performStart(Activity.java:6108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2478)
RecentNewsFragment.java
package org.cvsuimus.theflare;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.kosalgeek.android.json.JsonConverter;
import java.util.ArrayList;
import static org.cvsuimus.theflare.ConnectivityReceiver.isConnected;
public class RecentNewsFragment extends Fragment {
public RecentNewsFragment() {
}
final String TAG = "MainPageActivity";
private RecyclerView rvNewsArticle;
private SwipeRefreshLayout swipeRefreshLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_recent_news, container, false);
rvNewsArticle = (RecyclerView) view.findViewById(R.id.rvNewsArticle);
rvNewsArticle.setHasFixedSize(true);
LinearLayoutManager manager = new LinearLayoutManager(getActivity());
rvNewsArticle.setLayoutManager(manager);
loadNews();
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefresh);
swipeRefreshLayout.setColorSchemeResources(R.color.primaryColor, R.color.primaryTextColor, R.color.secondaryColor);
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
loadNews();
}
}, 1000);
}
});
return view;
}
private void loadNews() {
if (isConnected()) {
String url = "http://192.168.0.32/theflare/app/news.php";
StringRequest stringRequest = new StringRequest(Request.Method.GET,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response);
ArrayList<News> newsList = new JsonConverter<News>()
.toArrayList(response, News.class);
final NewsArticleAdapter adapter = new NewsArticleAdapter(getActivity(), newsList);
rvNewsArticle.setAdapter(adapter);
swipeRefreshLayout.setRefreshing(false);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error != null) {
snackInternet();
}
}
}
);
MySingleton.getInstance(getActivity()).addToRequestQueue(stringRequest);
}
else {
snackInternet();
}
}
private void snackInternet() {
Snackbar snackbar = Snackbar
.make(getView(), "No Internet Connectivity", Snackbar.LENGTH_INDEFINITE)
.setAction("Retry", new View.OnClickListener() {
@Override
public void onClick(View view) {
loadNews();
Snackbar snackbar1 = Snackbar.make(view, "Connected to the Internet!", Snackbar.LENGTH_SHORT);
snackbar1.show();
}
});
snackbar.show();
}
}
isConnected方法
public static boolean isConnected() {
ConnectivityManager
cm = (ConnectivityManager) MyApplication.getInstance().getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null
&& activeNetwork.isConnectedOrConnecting();
}