android.content.pm.PackageManager$NameNotFoundException: - java

I am creating an app for home automation. When the app is running on smartphone(os kitkat) I am getting the exception:
android.content.pm.PackageManager$NameNotFoundException:
This is the log
09-21 17:55:25.299 2550-2550/amrita.edu.ha W/System.err: android.content.pm.PackageManager$NameNotFoundException:
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.app.ApplicationPackageManager.getApplicationInfo(ApplicationPackageManager.java:227)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.app.ApplicationPackageManager.getResourcesForApplication(ApplicationPackageManager.java:786)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.widget.TextView.<init>(TextView.java:1323)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.widget.EditText.<init>(EditText.java:60)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:60)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.support.v7.widget.AppCompatEditText.<init>(AppCompatEditText.java:56)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:112)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:980)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1039)
09-21 17:55:25.300 2550-2550/amrita.edu.ha W/System.err: at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at amrita.edu.ha.AddUser.onCreate(AddUser.java:55)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.app.Activity.performCreate(Activity.java:5292)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
09-21 17:55:25.301 2550-2550/amrita.edu.ha W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at android.app.ActivityThread.access$800(ActivityThread.java:151)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at android.os.Handler.dispatchMessage(Handler.java:110)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at android.os.Looper.loop(Looper.java:193)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5292)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
09-21 17:55:25.302 2550-2550/amrita.edu.ha W/System.err: at dalvik.system.NativeStart.main(Native Method)
The code is below
public class AddUser extends AppCompatActivity { //implements MultiSelectionSpinner.OnMultipleItemsSelectedListener
private EditText et,et1,et2,et3,et4;
private Button btn;
private Spinner spinner;
private ImageButton imgtbn;
int i=0;
List<String> list = new ArrayList<>();
List<String> mylist=new ArrayList<>();
List<Integer>nlist=new ArrayList<>();
Integer myIntArray;
HashMap<String,Integer> map=new HashMap<String, Integer>();
Object objh="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
**setContentView(R.layout.adduser);**
ClientResource clientResource=new ClientResource("http://10.30.9.208:8080/api/users/view_group");
clientResource.setChallengeResponse(ChallengeScheme.HTTP_BASIC,
"admin", "admin");
clientResource.get();
Response response= clientResource.getResponse();
System.out.println("Response from server is ----> "+response.getEntityAsText());
JSONObject obj= null;
try {
obj = new JSONObject(response.getEntityAsText());
String strMessage = (String) obj.get("message");
JSONArray jarray = new JSONArray(strMessage);
for (int i = 0; i < jarray.length(); i++) {
JSONObject obj1 = jarray.getJSONObject(i);
list.add((String) obj1.get("group_name"));
map.put((String)obj1.get("group_name"),(Integer)obj1.get("group_id"));
}
}catch(JSONException e){
e.printStackTrace();
}
The exception points to the statement enclosed in two stars.

Related

Android Application Crashes Custom Listview while running getView part

I am creating a custom adapter using data from the web.
Below is what I've been working on. The logcat says it's having problem at
view = layoutInflater.inflate(R.layout.article_each, null);
in the getView part. This is line 49 of ArticleListAdapter, as indicated in the error line below logcat.
The full logcat around the error is like below. (Is there anyway I can get a more detailed error log?)
* I updated the log below! (please let me know if this is still not the right log to provide.)
04-17 22:57:53.408 22401-22401/org.androidtown.slidingexercise20180403 D/InputTransport: Input channel constructed: fd=102
04-17 22:57:53.408 22401-22401/org.androidtown.slidingexercise20180403 D/InputTransport: Input channel destroyed: fd=85
04-17 22:57:53.419 22401-22401/org.androidtown.slidingexercise20180403 D/ViewRootImpl#64962f[Toast]: ThreadedRenderer.create() translucent=true
04-17 22:57:53.425 22401-22401/org.androidtown.slidingexercise20180403 D/InputTransport: Input channel constructed: fd=98
04-17 22:57:53.427 22401-22401/org.androidtown.slidingexercise20180403 D/ViewRootImpl#64962f[Toast]: setView = android.widget.LinearLayout{7f0743c V.E...... ......I. 0,0-0,0} touchMode=true
04-17 22:57:53.430 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup)' on a null object reference
04-17 22:57:53.438 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at org.androidtown.slidingexercise20180403.ArticleListAdapter.getView(ArticleListAdapter.java:49)
04-17 22:57:53.438 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.AbsListView.obtainView(AbsListView.java:3238)
04-17 22:57:53.438 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.ListView.makeAndAddView(ListView.java:2147)
04-17 22:57:53.438 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.ListView.fillDown(ListView.java:767)
04-17 22:57:53.438 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.ListView.fillFromTop(ListView.java:833)
04-17 22:57:53.438 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.ListView.layoutChildren(ListView.java:1900)
04-17 22:57:53.438 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.AbsListView.onLayout(AbsListView.java:3008)
04-17 22:57:53.438 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.View.layout(View.java:18804)
04-17 22:57:53.439 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:5951)
04-17 22:57:53.439 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
04-17 22:57:53.439 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
04-17 22:57:53.439 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
04-17 22:57:53.439 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.View.layout(View.java:18804)
04-17 22:57:53.439 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:5951)
04-17 22:57:53.439 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
04-17 22:57:53.439 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
04-17 22:57:53.439 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.View.layout(View.java:18804)
04-17 22:57:53.439 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:5951)
04-17 22:57:53.439 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
04-17 22:57:53.440 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
04-17 22:57:53.440 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
04-17 22:57:53.440 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.View.layout(View.java:18804)
04-17 22:57:53.440 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:5951)
04-17 22:57:53.440 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
04-17 22:57:53.440 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
04-17 22:57:53.440 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.View.layout(View.java:18804)
04-17 22:57:53.440 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:5951)
04-17 22:57:53.440 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
04-17 22:57:53.440 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
04-17 22:57:53.441 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
04-17 22:57:53.441 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.View.layout(View.java:18804)
04-17 22:57:53.441 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:5951)
04-17 22:57:53.441 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
04-17 22:57:53.441 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
04-17 22:57:53.441 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at com.android.internal.policy.DecorView.onLayout(DecorView.java:884)
04-17 22:57:53.441 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.View.layout(View.java:18804)
04-17 22:57:53.441 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.ViewGroup.layout(ViewGroup.java:5951)
04-17 22:57:53.441 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2659)
04-17 22:57:53.442 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2375)
04-17 22:57:53.442 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1526)
04-17 22:57:53.442 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7102)
04-17 22:57:53.442 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
04-17 22:57:53.442 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.Choreographer.doCallbacks(Choreographer.java:702)
04-17 22:57:53.442 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.Choreographer.doFrame(Choreographer.java:638)
04-17 22:57:53.443 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
04-17 22:57:53.443 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.os.Handler.handleCallback(Handler.java:751)
04-17 22:57:53.443 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-17 22:57:53.443 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.os.Looper.loop(Looper.java:154)
04-17 22:57:53.443 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6682)
04-17 22:57:53.443 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-17 22:57:53.443 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
04-17 22:57:53.443 22401-22401/org.androidtown.slidingexercise20180403 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
04-17 22:57:53.445 22401-22401/org.androidtown.slidingexercise20180403 D/AndroidRuntime: Shutting down VM
04-17 22:57:53.451 22401-22401/org.androidtown.slidingexercise20180403 E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.androidtown.slidingexercise20180403, PID: 22401
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at org.androidtown.slidingexercise20180403.ArticleListAdapter.getView(ArticleListAdapter.java:55)
at android.widget.AbsListView.obtainView(AbsListView.java:3238)
at android.widget.ListView.makeAndAddView(ListView.java:2147)
at android.widget.ListView.fillDown(ListView.java:767)
at android.widget.ListView.fillFromTop(ListView.java:833)
at android.widget.ListView.layoutChildren(ListView.java:1900)
at android.widget.AbsListView.onLayout(AbsListView.java:3008)
at android.view.View.layout(View.java:18804)
at android.view.ViewGroup.layout(ViewGroup.java:5951)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:18804)
at android.view.ViewGroup.layout(ViewGroup.java:5951)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:18804)
at android.view.ViewGroup.layout(ViewGroup.java:5951)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:18804)
at android.view.ViewGroup.layout(ViewGroup.java:5951)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:18804)
at android.view.ViewGroup.layout(ViewGroup.java:5951)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:18804)
at android.view.ViewGroup.layout(ViewGroup.java:5951)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:884)
at android.view.View.layout(View.java:18804)
at android.view.ViewGroup.layout(ViewGroup.java:5951)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2659)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2375)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1526)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7102)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
at android.view.Choreographer.doCallbacks(Choreographer.java:702)
at android.view.Choreographer.doFrame(Choreographer.java:638)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
I've tried many different ways but still can't figure out what has gone wrong. I would be grateful for any suggestion and comment! Thank you!
Below is the custom adapter I am creating,
package org.androidtown.slidingexercise20180403;
import android.app.Activity;
import android.content.Context;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
class ArticleListAdapter extends BaseAdapter implements ListAdapter {
Context context;
ArrayList<HashMap<String, String>> articleList;
LayoutInflater layoutInflater;
public ArticleListAdapter (Context context, ArrayList<HashMap<String, String>> articleList) {
super();
this.articleList = articleList;
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
#Override public int getCount() {
if(articleList==null)
return 0;
else
return articleList.size();
}
#Override public HashMap<String, String> getItem(int position) {
if(null==articleList) return null;
else
return articleList.get(position);
}
#Override public long getItemId(int position) {
return position;
}
#Override public View getView(int position, View view, ViewGroup parent) {
view = layoutInflater.inflate(R.layout.article_each, null);
TextView titleView =(TextView)view.findViewById(R.id.title);
HashMap<String, String> eacharticle = articleList.get(position);
if(eacharticle != null ) {
String title = eacharticle.get("title");
titleView.setText(title);
}
return view;
}
}
and below is ArticleList activity.
package org.androidtown.slidingexercise20180403;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import static java.lang.Integer.parseInt;
public class ArticleList extends AppCompatActivity {
Intent intent;
String articlelist;
JSONArray jsonArray;
ArrayList<HashMap<String, String>> articleList;
ListView ArticleListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.article_list);
intent = getIntent();
articlelist = intent.getStringExtra("articlelist");
articleList = new ArrayList<HashMap<String, String>>();
ArticleListView = (ListView) findViewById(R.id.articlelistbox);
getArticleList(articlelist);
}
public void getArticleList(String url) {
class GetDataJSON extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
String uri = "http://www.critiquers.org/android/articlelist.php?articlelist="+params[0];
BufferedReader bufferedReader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
#Override
protected void onPostExecute(String result) {
try {
jsonArray = new JSONArray(result);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);
String no = c.getString("no");
String url = c.getString("url");
String title = c.getString("title");
String date_mktime = c.getString("date_mktime");
String media = c.getString("media");
String media_no = c.getString("media_no");
String author = c.getString("author");
String author_no = c.getString("author_no");
String facebook_no= c.getString("facebook_no");
String rep_image= c.getString("rep_image");
HashMap<String, String> addarticle = new HashMap<String, String>();
addarticle.put("no", "no");
addarticle.put("url", "url");
addarticle.put("title", "title");
addarticle.put("media", "media");
addarticle.put("media_no", "media_no");
addarticle.put("author", "author");
addarticle.put("author_no", "author_no");
addarticle.put("date_mktime", "date_mktime");
addarticle.put("facebook_no", "facebook_no");
addarticle.put("rep_image", "rep_image");
articleList.add(addarticle);
}
Integer size = articleList.size();
String sizeTxt = size.toString();
Toast.makeText (ArticleList.this, sizeTxt, Toast.LENGTH_LONG).show();
//아래에 정의될 커스텀어댑터 클래스로 어댑터를 설졍한다
ArticleListAdapter articleadapter = new ArticleListAdapter(
ArticleList.this,
articleList
);
ArticleListView.setAdapter(articleadapter);
} catch (JSONException e) {
e.printStackTrace();
}//end try catch
}//end onPostExecute
}//end GetDataJSON
GetDataJSON g = new GetDataJSON();
g.execute(url);
}//end getArticleList
public void xClicked(View v){
finish();
}
}
#Override public View getView(int position, View view, ViewGroup parent) {
view = layoutInflater.inflate(R.layout.article_each,parent,false);//the good way
TextView titleView =(TextView)view.findViewById(R.id.title);
HashMap<String, String> eacharticle = articleList.get(position);
if(eacharticle != null ) {
String title = eacharticle.get("title");
titleView.setText(title);
}
return view;
}
learn more here
for this error
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at org.androidtown.slidingexercise20180403.ArticleListAdapter.getView(ArticleListAdapter.java:55)
at android.widget.AbsListView.obtainView(AbsListView.java:3238)
at android.widget.ListView.makeAndAddView(ListView.java:2147)
at android.widget.ListView.fillDown(ListView.java:767)
at android.widget.ListView.fillFromTop(ListView.java:833)
at android.widget.ListView.layoutChildren(ListView.java:1900)
at android.widget.AbsListView.onLayout(AbsListView.java:3008)
at android.view.View.layout(View.java:18804)
at android.view.ViewGroup.layout(ViewGroup.java:5951)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:18804)
at android.view.ViewGroup.layout(ViewGroup.java:5951)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:18804)
at android.view.ViewGroup.layout(ViewGroup.java:5951)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:18804)
at android.view.ViewGroup.layout(ViewGroup.java:5951)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:18804)
at android.view.ViewGroup.layout(ViewGroup.java:5951)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:18804)
at android.view.ViewGroup.layout(ViewGroup.java:5951)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:884)
at android.view.View.layout(View.java:18804)
at android.view.ViewGroup.layout(ViewGroup.java:5951)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2659)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2375)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1526)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7102)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
at android.view.Choreographer.doCallbacks(Choreographer.java:702)
at android.view.Choreographer.doFrame(Choreographer.java:638)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Replace this on getView method
view = layoutInflater.inflate(R.layout.article_each, null);
to
view=LayoutInflater.from(parent.getContext()).inflate(R.layout.article_each, parent, false);
Hope this will help

Firebase notification not sending from Java code

I can successfully send notifications from the Firebase console to my device, but when I try to do it from Java code, I get a very unhelpful error:
04-12 20:18:09.284 17549-17549/com.package.example W/System.err: at com.package.example.FCMNotification.pushFCMNotification(FCMNotification.java:102)
Here is my FCMNotification Class
public class FCMNotification {
public final static String AUTH_KEY_FCM = "FCM_SERVERKEY";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
public static void pushFCMNotification(String deviceToken, String title, String body)
throws IOException, JSONException {
String authKey = AUTH_KEY_FCM; // You FCM AUTH key
String FMCurl = API_URL_FCM;
URL url = new URL(FMCurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization","key="+authKey);
conn.setRequestProperty("Content-Type","application/json");
JSONObject json = new JSONObject();
json.put("to",deviceToken.trim());
JSONObject info = new JSONObject();
info.put("title", title); // Notification title
info.put("body", body); // Notification body
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
}
}
And I call this class in my activity like this
FCMNotification obj = new FCMNotification();
obj.pushFCMNotification("DEVICE_TOKEN", "TITLE", "BODY");
EDIT
My app might be having trouble connecting to firebase server itself, so I have included my manifest and Gradle:
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.package.example">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/globalicon" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/cardview_shadow_start_color" />
<service android:name="com.package.example.PushNotification"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name="com.package.example.FCMService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>
Gradle:
apply plugin: 'com.android.application'
android {
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/io.netty.versions.properties'
exclude 'META-INF/INDEX.LIST'
}
signingConfigs {
config {
}
}
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
multiDexEnabled true
applicationId "com.gespanet.global"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 29
versionName "1.2"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
disable 'MissingTranslation'
disable 'ExtraTranslation'
}
dexOptions {
javaMaxHeapSize "4g"
}
repositories {
mavenCentral()
}
dependencies {
compile ('com.google.apis:google-api-services-translate:v2-rev49-1.22.0') {
exclude group: 'com.google.guava'
}
compile ('com.google.cloud:google-cloud-translate:0.11.0-alpha') {
exclude group: 'io.grpc', module: 'grpc-all'
exclude group: 'com.google.protobuf', module: 'protobuf-java'
exclude group: 'com.google.api-client', module: 'google-api-client-appengine'
}
compile 'com.google.android.gms:play-services:10.2.1'
compile 'com.google.firebase:firebase-core:10.2.0'
compile 'com.google.firebase:firebase-messaging:10.2.0'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.parse.bolts:bolts-tasks:1.3.0'
compile 'com.parse:parse-android:1.13.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.facebook.android:facebook-android-sdk:4.+'
}
configurations.all {
exclude group: 'org.json', module: 'json'
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
}
apply plugin: 'com.google.gms.google-services'
FULL ERROR LOG CAT FROM TRY/CATCHEXCEPTION:
04-12 23:31:56.027 12815-12815/com.gespanet.global W/System.err: android.os.NetworkOnMainThreadException
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:215)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:220)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:176)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:108)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:500)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:483)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:465)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:371)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:249)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:52)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6397)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6389)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:971)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:482)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:418)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:540)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:64)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6397)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6389)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.037 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-12 23:31:56.037 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: Hongbao
04-12 23:31:56.037 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
04-12 23:31:56.037 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: Hongbao
04-12 23:31:56.037 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
04-12 23:31:56.047 12815-12815/com.gespanet.global I/push: Error is here
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: android.os.NetworkOnMainThreadException
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:215)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:220)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:176)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:108)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:500)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:483)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:465)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:371)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:249)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:52)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6397)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6389)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:971)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:482)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:418)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:540)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:64)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6397)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$2$1.done(IndividualFeed.java:6389)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.047 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-12 23:31:56.067 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: Hongbao
04-12 23:31:56.067 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
04-12 23:31:56.067 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: Hongbao
04-12 23:31:56.067 12815-12815/com.gespanet.global I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
04-12 23:31:56.067 12815-12815/com.gespanet.global I/push: Error is here
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: android.os.NetworkOnMainThreadException
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.lookupHostByName(InetAddress.java:431)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:215)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:220)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:176)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:108)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:500)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:483)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:465)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:371)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:476)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:118)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:249)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:52)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$1$1.done(IndividualFeed.java:6366)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$1$1.done(IndividualFeed.java:6358)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'void com.android.okhttp.internal.http.Transport.writeRequestHeaders(com.android.okhttp.Request)' on a null object reference
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:971)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:482)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:418)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:540)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:25)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.FCMNotification.pushFCMNotification(FCMNotification.java:64)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$1$1.done(IndividualFeed.java:6366)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.gespanet.global.IndividualFeed$214$1$1$3$1$1$1.done(IndividualFeed.java:6358)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:116)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.os.Looper.loop(Looper.java:148)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7223)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
04-12 23:31:56.067 12815-12815/com.gespanet.global W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
To Send a Notification with Java. Implement this class:
public class FCMNotification {
// Method to send Notifications from server to client end.
public final static String AUTH_KEY_FCM = "YOUR_SERVERKEY";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
public static void pushFCMNotification(final String DeviceIdKey, final String title, final String body) throws Exception {
AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... voids) {
BufferedReader in = null;
String authKey = AUTH_KEY_FCM; // You FCM AUTH key
String FMCurl = API_URL_FCM;
URL url = null;
try {
url = new URL(FMCurl);
} catch (MalformedURLException e) {
Log.i("yoyoyo", "error: 1");
e.printStackTrace();
}
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
Log.i("yoyoyo", "error: 2");
e.printStackTrace();
}
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
try {
conn.setRequestMethod("POST");
} catch (ProtocolException e) {
Log.i("yoyoyo", "error: 3");
e.printStackTrace();
}
conn.setRequestProperty("Authorization","key="+authKey);
conn.setRequestProperty("Content-Type","application/json");
JSONObject json = new JSONObject();
try {
json.put("to",DeviceIdKey.trim());
} catch (JSONException e) {
Log.i("yoyoyo", "error: 4");
e.printStackTrace();
}
JSONObject info = new JSONObject();
try {
info.put("title", title); // Notification title
} catch (JSONException e) {
Log.i("yoyoyo", "error: 5");
e.printStackTrace();
}
try {
info.put("body", body); // Notification body
} catch (JSONException e) {
Log.i("yoyoyo", "error: 6");
e.printStackTrace();
}
try {
json.put("notification", info);
} catch (JSONException e) {
Log.i("yoyoyo", "error: 7");
e.printStackTrace();
}
OutputStreamWriter wr = null;
try {
wr = new OutputStreamWriter(conn.getOutputStream());
} catch (IOException e) {
Log.i("yoyoyo", "error: 8");
e.printStackTrace();
}
try {
wr.write(json.toString());
} catch (IOException e) {
Log.i("yoyoyo", "error: 9");
e.printStackTrace();
}
try {
wr.flush();
} catch (IOException e) {
Log.i("yoyoyo", "error: 10");
e.printStackTrace();
}
try {
conn.getInputStream();
} catch (IOException e) {
Log.i("yoyoyo", "error: 11");
e.printStackTrace();
}
return null;
}
};
asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}

Android Studio - How do I retrieve the contents of a web page?

So I've been tasked with creating an android app which will be using web services frequently.
I'm new to android and java but I cant see why this isn't working. Can someone help out? At this point all I'm trying to do is log the html code of a website, in this case Google.
MainActivity.java
package com.dummies.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
import com.dummies.myapplication.WebService;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String contents = WebService.getContents("http://www.google.com", "utf-8");
Log.d("Jon", contents);
setContentView(R.layout.activity_main);
}
public void invokeWS(){
}
}
WebService.java
package com.dummies.myapplication;
import android.util.Log;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import android.util.Log;
class WebService {
public static String getContents(String url, String encodeType) {
URL u;
StringBuilder builder = new StringBuilder();
try {
u = new URL(url);
try {
BufferedReader theHTML = new BufferedReader(new InputStreamReader(u.openStream(), encodeType));
String thisLine;
while ((thisLine = theHTML.readLine()) != null) {
builder.append(thisLine).append("\n");
}
} catch (Exception e) {
Log.d("Jon", "Error 2");
}
} catch (MalformedURLException e) {
Log.d("Jon", "Error");
System.err.println(url + " is not a parseable URL");
System.err.println(e);
}
return builder.toString();
}
}
I keep getting the Error 2 coming up in the log, why is this? can someone help get this code working and logging the output.
Thanks!
Stack:
02-03 23:41:56.261 2297-2297/com.dummies.myapplication D/Jon: Stack Trace:
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: android.os.NetworkOnMainThreadException
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:752)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.Network$1.resolveInetAddresses(Network.java:29)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:187)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:156)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:98)
02-03 23:41:56.262 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.http.HttpEngine.createNextConnection(HttpEngine.java:345)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:328)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:246)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:457)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:405)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:243)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at java.net.URL.openStream(URL.java:1057)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.dummies.myapplication.WebService.getContents(WebService.java:22)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.dummies.myapplication.MainActivity.onCreate(MainActivity.java:23)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.app.Activity.performCreate(Activity.java:6662)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.app.ActivityThread.-wrap12(ActivityThread.java)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.os.Looper.loop(Looper.java:154)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6077)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at java.lang.reflect.Method.invoke(Native Method)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
02-03 23:41:56.263 2297-2297/com.dummies.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
02-03 23:41:56.527 1278-1484/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
[ 02-03 23:41:56.529 2297: 2297 D/ ]
HostConnection::get() New Host Connection established 0xa4a93200, tid 2297
[ 02-03 23:41:56.530 2297: 2297 W/ ]
Process pipe failed
02-03 23:41:56.556 1278-1318/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
[ 02-03 23:41:56.557 2297: 2413 D/ ]
HostConnection::get() New Host Connection established 0xa4a93600, tid 2413
02-03 23:41:56.563 2297-2413/com.dummies.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
02-03 23:41:56.563 2297-2413/com.dummies.myapplication D/OpenGLRenderer: Swap behavior 1
02-03 23:41:56.564 1278-1318/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
02-03 23:41:56.572 1278-1318/? D/gralloc_ranchu: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer
02-03 23:41:56.594 2297-2413/com.dummies.myapplication E/EGL_emulation: tid 2413: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
02-03 23:41:56.594 2297-2413/com.dummies.myapplication W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x98224160, error=EGL_BAD_MATCH
02-03 23:41:56.603 2271-2271/com.google.android.googlequicksearchbox:search W/art: Long monitor contention with owner Binder:2271_1 (2283) at void java.lang.Object.wait!()(Object.java:-2) waiters=0 in java.lang.Object a.a.c.get() for 470ms
02-03 23:41:56.641 1569-1592/system_process I/ActivityManager: Displayed com.dummies.myapplication/.MainActivity: +6s119ms
02-03 23:41:56.655 1569-1693/system_process I/WindowManager: Destroying surface Surface(name=com.android.launcher3/com.android.launcher3.Launcher) called by com.android.server.wm.WindowStateAnimator.destroySurface:2014 com.android.server.wm.WindowStateAnimator.destroySurfaceLocked:881 com.android.server.wm.WindowState.destroyOrSaveSurface:2073 com.android.server.wm.AppWindowToken.destroySurfaces:363 com.android.server.wm.AppWindowToken.notifyAppStopped:389 com.android.server.wm.WindowManagerService.notifyAppStopped:4456 com.android.server.am.ActivityStack.activityStoppedLocked:1252 com.android.server.am.ActivityManagerService.activityStopped:6895
02-03 23:41:56.669 2271-2414/com.google.android.googlequicksearchbox:search W/ModelDownloadController: Cannot find any speech config location.
The problem is that Android doesn't allow you to execute Network Operations on the same Thread where UIOperations happen.
In order to connect to a network, you must create a different Thread using AsyncTasks or Services.
Here's a more detailed answer about Networks on Android: How to fix android.os.NetworkOnMainThreadException?
Maybe You should use Jsoup Library. It is super cool. You can find more information here.
https://jsoup.org/

NullPointerException while storing value of spinner in a string array [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am trying to create a fill in the blanks game. I have given the options to choose from in a spinner dropdown (10 spinner for 10 blanks). Till now its working. But to calculate score, I want to store the selected options in a string array. Here I am getting a NullPointerException.
package com.example.harimohan.myapplication;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
public class StartPage extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
TextView substring1;
TextView substring2;
TextView substring3;
TextView substring4;
TextView substring5;
TextView substring6;
TextView substring7;
TextView substring8;
TextView substring9;
TextView substring10;
TextView substring11;
Spinner blank1 ;
Spinner blank2 ;
Spinner blank3 ;
Spinner blank4 ;
Spinner blank5 ;
Spinner blank6 ;
Spinner blank7 ;
Spinner blank8 ;
Spinner blank9 ;
Spinner blank10;
String[] subs = new String[20] ;
String[] words = "batsmen domestically scored ranked appearances previously outstanding announced recipient aviation".split(" ");
String[] ans = new String[10];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_page);
blank1 = (Spinner) findViewById(R.id.Blank1);
Spinner blank2 = (Spinner) findViewById(R.id.Blank2);
Spinner blank3 = (Spinner) findViewById(R.id.Blank3);
Spinner blank4 = (Spinner) findViewById(R.id.Blank4);
Spinner blank5 = (Spinner) findViewById(R.id.Blank5);
Spinner blank6 = (Spinner) findViewById(R.id.Blank6);
Spinner blank7 = (Spinner) findViewById(R.id.Blank7);
Spinner blank8 = (Spinner) findViewById(R.id.Blank8);
Spinner blank9 = (Spinner) findViewById(R.id.Blank9);
Spinner blank10 = (Spinner) findViewById(R.id.Blank10);
blank1.setOnItemSelectedListener(this );
blank2.setOnItemSelectedListener( this);
blank3.setOnItemSelectedListener( this);
blank4.setOnItemSelectedListener( this);
blank5.setOnItemSelectedListener( this);
blank6.setOnItemSelectedListener( this);
blank7.setOnItemSelectedListener( this);
blank8.setOnItemSelectedListener( this);
blank9.setOnItemSelectedListener( this);
blank10.setOnItemSelectedListener( this);
substring1 = (TextView) findViewById(R.id.subs1);
substring2 = (TextView) findViewById(R.id.subs2);
substring3 = (TextView) findViewById(R.id.subs3);
substring4 = (TextView) findViewById(R.id.subs4);
substring5 = (TextView) findViewById(R.id.subs5);
substring6 = (TextView) findViewById(R.id.subs6);
substring7 = (TextView) findViewById(R.id.subs7);
substring8 = (TextView) findViewById(R.id.subs8);
substring9 = (TextView) findViewById(R.id.subs9);
substring10= (TextView) findViewById(R.id.subs10);
substring11= (TextView) findViewById(R.id.subs11);
new jsontask(). execute("https://en.wikipedia.org/w/api.php?action=query&prop=extracts&explaintext=&titles=Sachin_Tendulkar&formatversion=2&format=json");
List<String> categories = new ArrayList<String>();
categories.add("Select Answer");
categories.add(words[8]);
categories.add(words[1]);
categories.add(words[4]);
categories.add(words[3]);
categories.add(words[7]);
categories.add(words[5]);
categories.add(words[6]);
categories.add(words[0]);
categories.add(words[2]);
categories.add(words[9]);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
blank1.setAdapter(dataAdapter);
blank2.setAdapter(dataAdapter);
blank3.setAdapter(dataAdapter);
blank4.setAdapter(dataAdapter);
blank5.setAdapter(dataAdapter);
blank6.setAdapter(dataAdapter);
blank7.setAdapter(dataAdapter);
blank8.setAdapter(dataAdapter);
blank9.setAdapter(dataAdapter);
blank10.setAdapter(dataAdapter);
/*ans[0] = blank1.getSelectedItem().toString();
ans[1] = blank2.getSelectedItem().toString();
ans[2] = blank3.getSelectedItem().toString();
ans[3]= blank4.getSelectedItem().toString();
ans[4] = blank5.getSelectedItem().toString();
ans[5] = blank6.getSelectedItem().toString();
ans[6] = blank7.getSelectedItem().toString();
ans[7] = blank8.getSelectedItem().toString();
ans[8] = blank9.getSelectedItem().toString();
ans[9] = blank10.getSelectedItem().toString();*/
}
public int Score( String[] WordString, String[] AnsString){
for (int j=0;j<AnsString.length;j++) {
Log.d("Ans: " ,j +" : "+ AnsString[j]);
}
int scores=0;
for (int i=0;i<WordString.length;i++){
if (WordString[i].contains(AnsString[i])) {
scores++;
}
}
Log.d("scores",Integer.toString(scores));
return scores;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
try{
Spinner spinner = (Spinner) parent;
Log.d("spinner=",Integer.toString(spinner.getId()));
Log.d("R.id.Blank1=",Integer.toString(R.id.Blank1));
Log.d("Blank1 value=",blank1.getSelectedItem().toString());
/*if(spinner.getId() == R.id.Blank1)
{*/
String ans = blank1.getSelectedItem().toString();
Log.d("ans0 ",ans);
/*}
else if(spinner.getId() == R.id.Blank2)
{
ans[1] = blank2.getSelectedItem().toString();
Log.d("ans1 ",ans[1]);
}*/
}catch (Exception exception)
{
exception.printStackTrace();
}
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item +" at "+position+""+id, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
public class jsontask extends AsyncTask<String, String, String[]> {
#Override
protected String[] doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line ;
while ((line=reader.readLine())!= null) {
buffer.append(line);
}
String json = buffer.toString();
JSONObject parentObj = new JSONObject(json);
JSONObject query = parentObj.getJSONObject("query");
JSONArray pages = query.getJSONArray("pages");
JSONObject pagearray = pages.getJSONObject(0);
String extract = pagearray.getString("extract");
int lines = 0;
int pos = 0;
String print = null;
int x;
while ((pos = extract.indexOf(".", pos) + 1) != 0) {
lines++;
if(lines==10){
x= extract.indexOf("\n",pos);
print = extract.substring(0,x);
break;
}
}
String[] sentence = print.split(" ");
for (int j = 0 ; j < words.length;j++){
boolean flag= false;
for (int i = 0; i < sentence.length ; i++) {
if (sentence[i].equals(words[j])) {
/* System.out.println(words[j]+" is found at "+ i);
System.out.println("size of "+words[j]+"="+words[j].length());
*/
if (j==0){
System.out.println("end "+print.indexOf(words[j]));
System.out.println(print.substring(0,i));
subs[j]= print.substring(0,print.indexOf(words[j]));
System.out.println(print.indexOf(words[j]));
}
else{
int start = print.indexOf(words[j-1])+ words[j-1].length();
/*System.out.println(print.indexOf(words[j-1])+"+" +words[j-1].length());
System.out.println("start"+start);
System.out.println("end"+print.indexOf(words[j]));*/
subs[j]= print.substring(start,print.indexOf(words[j]));
if(j==9){
int n =print.indexOf(words[j])+ words[j].length();
subs[10]=print.substring(n);
}
}
flag= true;
break;
}
}
if (!flag ){
System.out.println("doesnt found");
}
}
return subs;
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e3) {
e3.printStackTrace();
}
}
return null;
}
#Override
protected void onPostExecute(String[] result) {
super.onPostExecute(result);
substring1.setText(subs[0]);
substring2.setText(subs[1]);
substring3.setText(subs[2]);
substring4.setText(subs[3]);
substring5.setText(subs[4]);
substring6.setText(subs[5]);
substring7.setText(subs[6]);
substring8.setText(subs[7]);
substring9.setText(subs[8]);
substring10.setText(subs[9]);
substring11.setText(subs[10]);
}
}
public void gotoScorePage(View view){
Score(words,ans);
Intent nextPage=new Intent(StartPage.this,ScorePage.class);
nextPage.putExtra("Score",Score(words,ans));
startActivity(nextPage);
}
}
This is logcat
07-13 12:33:18.360 19760-19760/com.example.harimohan.myapplication D/dalvikvm: VFY: replacing opcode 0x20 at 0x0016
07-13 12:33:18.810 19760-19760/com.example.harimohan.myapplication D/spinner=: 2131492976
07-13 12:33:18.810 19760-19760/com.example.harimohan.myapplication D/R.id.Blank1=: 2131492976
07-13 12:33:18.810 19760-19760/com.example.harimohan.myapplication W/System.err: java.lang.NullPointerException
07-13 12:33:18.810 19760-19760/com.example.harimohan.myapplication W/System.err: at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
07-13 12:33:18.810 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
07-13 12:33:18.820 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.access$200(AdapterView.java:48)
07-13 12:33:18.820 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
07-13 12:33:18.820 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
07-13 12:33:18.820 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 12:33:18.830 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Looper.loop(Looper.java:136)
07-13 12:33:18.830 19760-19760/com.example.harimohan.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
07-13 12:33:18.830 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-13 12:33:18.830 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-13 12:33:18.830 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-13 12:33:18.830 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-13 12:33:18.830 19760-19760/com.example.harimohan.myapplication W/System.err: at dalvik.system.NativeStart.main(Native Method)
07-13 12:33:18.840 19760-19760/com.example.harimohan.myapplication D/spinner=: 2131492978
07-13 12:33:18.840 19760-19760/com.example.harimohan.myapplication D/R.id.Blank1=: 2131492976
07-13 12:33:18.850 19760-19760/com.example.harimohan.myapplication W/System.err: java.lang.NullPointerException
07-13 12:33:18.850 19760-19760/com.example.harimohan.myapplication W/System.err: at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.access$200(AdapterView.java:48)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Looper.loop(Looper.java:136)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at dalvik.system.NativeStart.main(Native Method)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication D/spinner=: 2131492980
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication D/R.id.Blank1=: 2131492976
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: java.lang.NullPointerException
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.access$200(AdapterView.java:48)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Looper.loop(Looper.java:136)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-13 12:33:18.860 19760-19760/com.example.harimohan.myapplication W/System.err: at dalvik.system.NativeStart.main(Native Method)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication D/spinner=: 2131492982
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication D/R.id.Blank1=: 2131492976
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: java.lang.NullPointerException
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.access$200(AdapterView.java:48)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Looper.loop(Looper.java:136)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-13 12:33:18.880 19760-19760/com.example.harimohan.myapplication W/System.err: at dalvik.system.NativeStart.main(Native Method)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication D/spinner=: 2131492984
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication D/R.id.Blank1=: 2131492976
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: java.lang.NullPointerException
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.access$200(AdapterView.java:48)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Looper.loop(Looper.java:136)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at dalvik.system.NativeStart.main(Native Method)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication D/spinner=: 2131492986
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication D/R.id.Blank1=: 2131492976
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: java.lang.NullPointerException
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.access$200(AdapterView.java:48)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
07-13 12:33:18.890 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Looper.loop(Looper.java:136)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at dalvik.system.NativeStart.main(Native Method)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication D/spinner=: 2131492988
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication D/R.id.Blank1=: 2131492976
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: java.lang.NullPointerException
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView.access$200(AdapterView.java:48)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.handleCallback(Handler.java:733)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.os.Looper.loop(Looper.java:136)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5017)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invokeNative(Native Method)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at java.lang.reflect.Method.invoke(Method.java:515)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-13 12:33:18.900 19760-19760/com.example.harimohan.myapplication
there is :
at com.example.harimohan.myapplication.StartPage.onItemSelected(StartPage.java:174)
in line 174 you have choose and null value
maybe you should initilize blank1 in on create method .
You can try to check the where the null is in line 174 (the blank1.getSelectedItem() return the null value). It's helpful for you to solve the problem . Please forgive my English is poor.

json volley showing error android

JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Patient patient = new Patient();
patient.setTitle(obj.getString("id"));
patient.setThumbnailUrl(obj.getString("image"));
patientList.add(patient);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
}
Logcat
01-07 07:27:29.294 7938-7938/info.androidhive.customlistviewvolley D/MainActivity: [{"id":"g"},{"image":"http:\/\/192.168.0.101\/test\/1.png"}]
01-07 07:27:29.312 7938-7938/info.androidhive.customlistviewvolley W/System.err: org.json.JSONException: No value for image
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at org.json.JSONObject.getString(JSONObject.java:550)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at info.androidhive.customlistviewvolley.MainActivity$1.onResponse(MainActivity.java:72)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at info.androidhive.customlistviewvolley.MainActivity$1.onResponse(MainActivity.java:59)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at android.os.Looper.loop(Looper.java:135)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at java.lang.reflect.Method.invoke(Native Method)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: org.json.JSONException: No value for id
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at org.json.JSONObject.get(JSONObject.java:389)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at org.json.JSONObject.getString(JSONObject.java:550)
01-07 07:27:29.313 7938-7938/info.androidhive.customlistviewvolley W/System.err: at info.androidhive.customlistviewvolley.MainActivity$1.onResponse(MainActivity.java:71)
I can get data from the database but why does it still show that there is no value for the image?
I have checked that the url is right to get the photo. The following things are the error showed in the logcat. Please give me some helps. Thank you.
You get wrong JSONObject , try this
try {
JSONObject objId = response.getJSONObject(0);
JSONObject objImage = response.getJSONObject(1);
Patient patient = new Patient();
patient.setTitle(objId.getString("id"));
patient.setThumbnailUrl(objImage.getString("image"));
patientList.add(patient);
} catch (JSONException e) {
e.printStackTrace();
}
Look at your response and try this
JSONObject obj = response.getJSONObject(0);
JSONObject obj1 = response.getJSONObject(1);
Patient patient = new Patient();
patient.setTitle(obj.getString("id"));
patient.setThumbnailUrl(obj1.getString("image"));

Categories