Android Application Crashes Custom Listview while running getView part - java

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

Related

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/

android.content.pm.PackageManager$NameNotFoundException:

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.

Android java.lang.NullPointerException not able to populate the listview

I'm trying to populate a listview with some data from json.
Here are the errors I am getting:
04-17 12:54:25.573: E/JSON Parser(1169): Error parsing data org.json.JSONException:
of type org.json.JSONArray cannot be converted to JSONObject
04-17 12:54:26.223: E/AndroidRuntime(1169): FATAL EXCEPTION: main
04-17 12:54:26.223: E/AndroidRuntime(1169): Process: com.example.mysqltest, PID: 1169
04-17 12:54:26.223: E/AndroidRuntime(1169): java.lang.NullPointerException
04-17 12:54:26.223: E/AndroidRuntime(1169): at com.example.mysqltest.PropertyView$JSONParseP.onPostExecute(PropertyView.java:95)
04-17 12:54:26.223: E/AndroidRuntime(1169): at com.example.mysqltest.PropertyView$JSONParseP.onPostExecute(PropertyView.java:1)
04-17 12:54:26.223: E/AndroidRuntime(1169): at android.os.AsyncTask.finish(AsyncTask.java:632)
04-17 12:54:26.223: E/AndroidRuntime(1169): at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-17 12:54:26.223: E/AndroidRuntime(1169): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
04-17 12:54:26.223: E/AndroidRuntime(1169): at android.os.Handler.dispatchMessage(Handler.java:102)
04-17 12:54:26.223: E/AndroidRuntime(1169): at android.os.Looper.loop(Looper.java:136)
04-17 12:54:26.223: E/AndroidRuntime(1169): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-17 12:54:26.223: E/AndroidRuntime(1169): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 12:54:26.223: E/AndroidRuntime(1169): at java.lang.reflect.Method.invoke(Method.java:515)
04-17 12:54:26.223: E/AndroidRuntime(1169): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-17 12:54:26.223: E/AndroidRuntime(1169): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-17 12:54:26.223: E/AndroidRuntime(1169): at dalvik.system.NativeStart.main(Native Method)
Here is my Java file...
JSONArray array = null
public void onClick(View view)
new JSONParseP().execute()
private class JSONParseP extends AsyncTask<String, String, JSONObject>
protected JSONObject doInBackground(String... args)
JSONParserP jParser = new JSONParserP();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrlP(url);
return json;
protected void onPostExecute(JSONObject json)
pDialog.dismiss();
try {
// Getting JSON Array from URL
array = json.getJSONArray(null);
for(int i = 0; i < array.length(); i++){
JSONObject c = array.getJSONObject(i);
Any help I would highly appreciate it.
Thanks.
Try changing JSONObject c = array.getJSONObject(i); to JSONArray c = array.getJSONArray(i);

How do I create custom classes as subroutines

I may have my terms mixed up, but I'm creating an android app and I want to encapsulate some of the routine functions. For instance my actionBar. At first I had the code on all my activities and if I change one thing I have to change it else where. I want to create a NavigationActionBarManager.java file to handle the inital setup, onNavigationListener, setListNavigationCallbacks, etc.
Here's the class so far:
import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.SpinnerAdapter;
public class NavigationActionBarManager extends Activity {
public ActionBar actionBar = getActionBar(); // actionbar object
// METHOD: display
public void display() {
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
}
// METHOD: inflate
public void inflate(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_adventurers_new, menu);
}
// METHOD: listen
public void listen() {
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.array_character_views, android.R.layout.simple_spinner_dropdown_item);
OnNavigationListener mOnNavigationListener = new OnNavigationListener() {
//String[] strings = getResources().getStringArray(R.array.array_character_views);
#Override
public boolean onNavigationItemSelected(int position, long itemId) {
Intent nextScreen = null;
switch(position) {
case 0:
break;
case 1:
nextScreen = new Intent(getApplicationContext(), AdventurersNewAbilitiesActivity.class);
break;
case 2:
break;
}
if(nextScreen != null) {
startActivity(nextScreen);
}
return false;
}
};
actionBar.setListNavigationCallbacks(mSpinnerAdapter, mOnNavigationListener);
}
}
Back in my activity class, I want to apply it as such:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adventurer_new_character);
navBar.display();
navBar.listen();
}
I'm running into an error and I don't understand LogCat's output. Here's the LogCat ouput:
04-17 23:12:01.110: E/AndroidRuntime(14013): FATAL EXCEPTION: main
04-17 23:12:01.110: E/AndroidRuntime(14013): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.app/com.app.AdventurersNewCharacterActivity}: java.lang.NullPointerException
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1903)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2004)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.ActivityThread.access$600(ActivityThread.java:132)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.os.Looper.loop(Looper.java:137)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.ActivityThread.main(ActivityThread.java:4580)
04-17 23:12:01.110: E/AndroidRuntime(14013): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 23:12:01.110: E/AndroidRuntime(14013): at java.lang.reflect.Method.invoke(Method.java:511)
04-17 23:12:01.110: E/AndroidRuntime(14013): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-17 23:12:01.110: E/AndroidRuntime(14013): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-17 23:12:01.110: E/AndroidRuntime(14013): at dalvik.system.NativeStart.main(Native Method)
04-17 23:12:01.110: E/AndroidRuntime(14013): Caused by: java.lang.NullPointerException
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.Activity.initActionBar(Activity.java:2071)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.Activity.getActionBar(Activity.java:2058)
04-17 23:12:01.110: E/AndroidRuntime(14013): at com.app.NavigationActionBarManager.<init>(NavigationActionBarManager.java:13)
04-17 23:12:01.110: E/AndroidRuntime(14013): at com.app.AdventurersNewCharacterActivity.<init>(AdventurersNewCharacterActivity.java:13)
04-17 23:12:01.110: E/AndroidRuntime(14013): at java.lang.Class.newInstanceImpl(Native Method)
04-17 23:12:01.110: E/AndroidRuntime(14013): at java.lang.Class.newInstance(Class.java:1319)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.Instrumentation.newActivity(Instrumentation.java:1025)
04-17 23:12:01.110: E/AndroidRuntime(14013): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1894)
04-17 23:12:01.110: E/AndroidRuntime(14013): ... 11 more
Have you implemented the onCreateOptionsMenu() method in your activity?
doc available at https://developer.android.com/guide/topics/ui/actionbar.html

Issue retrieving JSON in Android

I've looked at countless projects and tutorials and haven't been able to figure out why my app is crashing as soon as it is ran. It seems to run fine if i comment out the first line in the try catch "JSONObject dataObject = json.getJSONObject("data");" The JSON that i'm trying to parse is formatted as such:
{"recordcount":3,
"columnlist":"department,emailaddress,firstname,has_photos,job_full_title,job_title,kid,lastname,location,middlename,no_show_photo,phone,school",
"data":
{"department":["Public Safety","Information Technology","Information Technology"],
"emailaddress":["todd.mongeon#wne.edu","amutti#wne.edu","fatemeh.shams#wne.edu"],
"firstname":["Todd","Tony","Toffee"],
"has_photos":["Y","Y","N"],
"job_full_title":["Officer","Director, Administrative Information Systems","Sr.Technical Programmer/Analyst and Integ Specialist"],
"job_title":["Officer","Director, Administrative Information Systems","Sr.Technical Programmer/Analyst and Integ Specialist"],
"kid":[286899,2497,297411],
"lastname":["Mongeon","Mutti","Shams"],
"location":["PS","12 V","12 V"],
"middlename":["A.","M.",""],
"no_show_photo":["","",""],
"phone":["413-782-1207","413-782-1212","413-796-2398"],
"school":["Public Safety Group","Information Technology Group","Information Technology Group"]}}
My program looks like this (please excuse formatting / things that don't need to be there... it needs to be cleaned up from a lot of the things i've tried to get this to work..)
public class Directory_finalActivity extends ListActivity {
public static String url = "http://www1.wne.edu/webservices/directorydemo.cfc?wsdl&method=getEmployees&limit=25&firstname=to&outputtype=JSON";
// JSON array names
private static final String TAG_FIRST = "firstname";
private static final String TAG_LAST = "lastname";
private static final String TAG_SCHOOL = "school";
private static final String TAG_DEPARTMENT = "department";
private static final String TAG_EMAIL = "emailaddress";
private static final String TAG_PHONE = "phone";
private static final String TAG_JOB = "job_title";
private static final String TAG_LOCATION = "location";
//create JSON array
JSONArray firstNames = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
String[] firstNames = null;
try {
// Getting the data object
JSONObject dataObject = json.getJSONObject("data"); //This is the line that seems to be causing the error.
int numResults = json.getInt("recordcount");
/**
JSONArray firstNameArray = dataObject.getJSONArray("firstname");
JSONArray lastNameArray = dataObject.getJSONArray("lastname");
JSONArray schoolArray = dataObject.getJSONArray("school");
JSONArray departmentArray = dataObject.getJSONArray("department");
JSONArray eMailArray = dataObject.getJSONArray("emailaddress");
JSONArray phoneNumberArray = dataObject.getJSONArray("phone");
JSONArray jobTitleArray = dataObject.getJSONArray("job_title");
JSONArray locationArray = dataObject.getJSONArray("location");
//Loop through index of array(s)
for(int i = 0; i <= numResults; i++)
{
//Store value of JSONArray at index "i" into string variables.
String firstName = firstNameArray.getString(i);
String eMail = eMailArray.getString(i);
String phone = phoneNumberArray.getString(i);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_FIRST, firstName);
map.put(TAG_EMAIL, eMail);
map.put(TAG_PHONE, phone);
//add the hashmap to array list.
contactList.add(map);
}*/
}
catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
//ListAdapter adapter = new SimpleAdapter(this, contactList,
// R.layout.list_item,
// new String[] { TAG_FIRST, TAG_EMAIL, TAG_PHONE }, new int[] {
// R.id.name, R.id.email, R.id.mobile });
//setListAdapter(adapter);
}
}
My JSONParser class
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
I'm probably doing something incredibly stupid here, i'm new to this so give me a break please haha. I appreciate any info that you guys may have for me.
For curiosity sake the end goal is to search the database (first name, last name, school, and department) and put the results into a list view, when clicked on a window will pop up displaying other relevant information with a couple other features.
It seems every example i see the JSON output they receive from the url is formatted much differently, so i'm having trouble getting this done even with the amount of examples available (sorry for re-post).
Once again thank you for any assistance.
EDIT 1:
LOGCAT:
04-17 00:45:35.828: W/System.err(305): java.net.UnknownHostException: api.androidhive.info
04-17 00:45:35.828: W/System.err(305): at java.net.InetAddress.lookupHostByName(InetAddress.java:513)
04-17 00:45:35.838: W/System.err(305): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:278)
04-17 00:45:35.838: W/System.err(305): at java.net.InetAddress.getAllByName(InetAddress.java:242)
04-17 00:45:35.838: W/System.err(305): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
04-17 00:45:35.838: W/System.err(305): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-17 00:45:35.838: W/System.err(305): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-17 00:45:35.838: W/System.err(305): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
04-17 00:45:35.838: W/System.err(305): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-17 00:45:35.838: W/System.err(305): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-17 00:45:35.838: W/System.err(305): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-17 00:45:35.838: W/System.err(305): at directory.plus.JSONParser.getJSONFromUrl(JSONParser.java:39)
04-17 00:45:35.838: W/System.err(305): at directory.plus.Directory_finalActivity.onCreate(Directory_finalActivity.java:53)
04-17 00:45:35.849: W/System.err(305): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-17 00:45:35.849: W/System.err(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-17 00:45:35.849: W/System.err(305): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-17 00:45:35.849: W/System.err(305): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-17 00:45:35.849: W/System.err(305): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-17 00:45:35.849: W/System.err(305): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 00:45:35.849: W/System.err(305): at android.os.Looper.loop(Looper.java:123)
04-17 00:45:35.849: W/System.err(305): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-17 00:45:35.849: W/System.err(305): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 00:45:35.849: W/System.err(305): at java.lang.reflect.Method.invoke(Method.java:521)
04-17 00:45:35.858: W/System.err(305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-17 00:45:35.858: W/System.err(305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-17 00:45:35.858: W/System.err(305): at dalvik.system.NativeStart.main(Native Method)
04-17 00:45:35.858: E/Buffer Error(305): Error converting result java.lang.NullPointerException
04-17 00:45:35.868: E/JSON Parser(305): Error parsing data org.json.JSONException: End of input at character 0 of
04-17 00:45:35.868: D/AndroidRuntime(305): Shutting down VM
04-17 00:45:35.868: W/dalvikvm(305): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-17 00:45:35.888: E/AndroidRuntime(305): FATAL EXCEPTION: main
04-17 00:45:35.888: E/AndroidRuntime(305): java.lang.RuntimeException: Unable to start activity ComponentInfo{directory.plus/directory.plus.Directory_finalActivity}: java.lang.NullPointerException
04-17 00:45:35.888: E/AndroidRuntime(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.os.Looper.loop(Looper.java:123)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-17 00:45:35.888: E/AndroidRuntime(305): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 00:45:35.888: E/AndroidRuntime(305): at java.lang.reflect.Method.invoke(Method.java:521)
04-17 00:45:35.888: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-17 00:45:35.888: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-17 00:45:35.888: E/AndroidRuntime(305): at dalvik.system.NativeStart.main(Native Method)
04-17 00:45:35.888: E/AndroidRuntime(305): Caused by: java.lang.NullPointerException
04-17 00:45:35.888: E/AndroidRuntime(305): at directory.plus.Directory_finalActivity.onCreate(Directory_finalActivity.java:59)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
EDIT 2:
NEW LOGCAT OUTPUT:
04-17 01:26:12.899: E/JSON Parser(548): Error parsing data in JSONParser org.json.JSONException: Value <wddxPacket of type java.lang.String cannot be converted to JSONObject
04-17 01:26:12.899: D/AndroidRuntime(548): Shutting down VM
04-17 01:26:12.899: W/dalvikvm(548): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-17 01:26:12.919: E/AndroidRuntime(548): FATAL EXCEPTION: main
04-17 01:26:12.919: E/AndroidRuntime(548): java.lang.RuntimeException: Unable to start activity ComponentInfo{directory.plus/directory.plus.Directory_finalActivity}: java.lang.NullPointerException
04-17 01:26:12.919: E/AndroidRuntime(548): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.os.Looper.loop(Looper.java:123)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-17 01:26:12.919: E/AndroidRuntime(548): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 01:26:12.919: E/AndroidRuntime(548): at java.lang.reflect.Method.invoke(Method.java:521)
04-17 01:26:12.919: E/AndroidRuntime(548): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-17 01:26:12.919: E/AndroidRuntime(548): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-17 01:26:12.919: E/AndroidRuntime(548): at dalvik.system.NativeStart.main(Native Method)
04-17 01:26:12.919: E/AndroidRuntime(548): Caused by: java.lang.NullPointerException
04-17 01:26:12.919: E/AndroidRuntime(548): at directory.plus.Directory_finalActivity.onCreate(Directory_finalActivity.java:59)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-17 01:26:12.919: E/AndroidRuntime(548): ... 11 more
04-17 01:26:33.279: I/Process(548): Sending signal. PID: 548 SIG: 9
EDIT FINAL: Problem solved:
Needed to take a sub string of the result returned from the html and pass that into a json object
java.net.UnknownHostException: api.androidhive.info
It looks like your app can't access the network. Have you added:
<uses-permission
android:name="android.permission.INTERNET" />
to your code AndroidManifest.xml?

Categories