Issue retrieving JSON in Android - java
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?
Related
Fragment cannot be cast to android.content.Context
I want to connect my login page to MySQL PHP but I got some error here. This is my logcat: 807/com.aeu.mlibrary.mlibraryaeu E/AndroidRuntime: FATAL EXCEPTION: main Process: com.aeu.mlibrary.mlibraryaeu, PID: 1807 java.lang.ClassCastException: com.aeu.mlibrary.mlibraryaeu.LoginFragment cannot be cast to android.content.Context at com.kosalgeek.asynctask.PostResponseAsyncTask.<init>(PostResponseAsyncTask.java:284) at com.aeu.mlibrary.mlibraryaeu.LoginFragment.onClick(LoginFragment.java:82) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) and this is the error line in my loginFragment.java: #Override public void onClick(View v) { HashMap postData = new HashMap(); postData.put("mobile", "android"); postData.put("txtUsername", etUsername.getText().toString()); postData.put("txtPassword", etPassword.getText().toString()); PostResponseAsyncTask task = new PostResponseAsyncTask(LoginFragment.this, postData); task.execute("http://10.0.3.2/mlibrary/HTML/login.php"); } I need your help guys! Thank you.
Replace LoginFragment.this with getActvity() PostResponseAsyncTask task = new PostResponseAsyncTask(getActivity(), postData);
Replace LoginFragment.this with getContext() : PostResponseAsyncTask task = new PostResponseAsyncTask(getContext(), postData);
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
java.lang.ClassCastException: org.ksoap2.serialization.SoapObject in android
Hi i have developed one android application. The app purpose is retrieve data from mysql database and display in android device. This is my android code: public class RetailerActivity extends Activity { private final String NAMESPACE = "http://ws.testprops.com"; private final String URL = "http://krish.jelastic.servint.net/Retrieve/services/Fetch?wsdl"; private final String SOAP_ACTION = "http://ws.testprops.com/customerData"; private final String METHOD_NAME = "customerData"; /** Called when the activity is first created. */ #Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(request); HttpTransportSE ht = new HttpTransportSE(URL); try { ht.call(SOAP_ACTION, envelope); SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); SoapPrimitive s = response; String str = s.toString(); String resultArr[] = str.split("&");//Result string will split & store in an array TextView tv = new TextView(this); for(int i = 0; i<resultArr.length;i++){ tv.append(resultArr[i]+"\n\n"); } setContentView(tv); } catch (Exception e) { e.printStackTrace(); } }} If i have to run the app means blank screen only displayed.it is taking too long time nearly 1 hour also am getting following error on my android console window: 11-05 10:38:27.868: W/System.err(837): java.lang.ClassCastException: org.ksoap2.serialization.SoapObject 11-05 10:38:27.878: W/System.err(837): at com.retailer.client.RetailerActivity.onCreate(RetailerActivity.java:29) 11-05 10:38:27.878: W/System.err(837): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 11-05 10:38:27.878: W/System.err(837): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 11-05 10:38:27.878: W/System.err(837): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 11-05 10:38:27.878: W/System.err(837): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 11-05 10:38:27.878: W/System.err(837): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 11-05 10:38:27.878: W/System.err(837): at android.os.Handler.dispatchMessage(Handler.java:99) 11-05 10:38:27.878: W/System.err(837): at android.os.Looper.loop(Looper.java:123) 11-05 10:38:27.878: W/System.err(837): at android.app.ActivityThread.main(ActivityThread.java:4627) 11-05 10:38:27.878: W/System.err(837): at java.lang.reflect.Method.invokeNative(Native Method) 11-05 10:38:27.878: W/System.err(837): at java.lang.reflect.Method.invoke(Method.java:521) 11-05 10:38:27.878: W/System.err(837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 11-05 10:38:27.878: W/System.err(837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 11-05 10:38:27.888: W/System.err(837): at dalvik.system.NativeStart.main(Native Method) please help me.how can i resolve this error.
Change SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); to SoapObject response = (SoapObject)envelope.getResponse();
How to remove error when declaring an array of a class in Android
Basically, I'm making a reversi app for Android, and I am currently at the point of setting up the game board. The way I am intending to do this is to create a 2D array of the position class. Each position is represented by an imageview that is used as a button which represents a board position. Here is a snippet of the code that contains the error: setupBoard(board); .... public void setupBoard(Position board[][]) { for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { Log.d("Error","Error a"); board[x][y] = new Position(getApplicationContext()); Log.d("Error","Error b"); board[x][y].isPositionEmpty = true; } } } In the position class: public class Position { Context myContext; public Position(Context context) { Log.d("Error","Error c"); myContext = context; // TODO Auto-generated constructor stub } public boolean isPositionEmpty = true; public int positionID; public ImageView button = new ImageView(myContext); } The program gets to error a, but no further. The error in LogCat is: java.lang.NullPointerException Any help will be much appreciated. Thanks in advance! Exception: 02-20 16:52:20.820: ERROR/AndroidRuntime(365): FATAL EXCEPTION: main 02-20 16:52:20.820: ERROR/AndroidRuntime(365): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{trinity.hazard.reversi/trinity.hazard.reversi.SinglePlayerActivity}: java.lang.NullPointerException 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.os.Handler.dispatchMessage(Handler.java:99) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.os.Looper.loop(Looper.java:123) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.app.ActivityThread.main(ActivityThread.java:4627) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at java.lang.reflect.Method.invokeNative(Native Method) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at java.lang.reflect.Method.invoke(Method.java:521) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at dalvik.system.NativeStart.main(Native Method) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): Caused by: java.lang.NullPointerException 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at trinity.hazard.reversi.SinglePlayerActivity.(SinglePlayerActivity.java:24) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at java.lang.Class.newInstanceImpl(Native Method) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at java.lang.Class.newInstance(Class.java:1429) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) 02-20 16:52:20.820: ERROR/AndroidRuntime(365): ... 11 more
In your Position class, try moving the line where you create the ImageView into the constructor, but AFTER initializing myContext.