Here is the code I had used but when I enter the page, it will automatically stopped. I have used volley request object and it does not support facebook login also. I have used custom list view valley for web service .
public void reload()
{
//final AlertDialog alertDialog = new AlertDialog.Builder(Editprofile.this).create();
Log.d("url23",url23);
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url23,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
String TAG = null;
Log.d(TAG, response.toString());
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
/* String fn= obj.getString("Fname");
String ln= obj.getString("Lname");
String em= obj.getString("email");
String ph= obj.getString("phnum");
String lk= obj.getString("live");
String wr= obj.getString("work");
String de= obj.getString("describe");
Log.d("OUTPUT IS",fn);*/
fname.setText(obj.getString("Fname"));
lname.setText(obj.getString("Lname"));
/* email.setText(em,TextView.BufferType.EDITABLE);
phone.setText(ph,TextView.BufferType.EDITABLE);
live.setText(lk,TextView.BufferType.EDITABLE);
work.setText(wr,TextView.BufferType.EDITABLE);
desc.setText(de,TextView.BufferType.EDITABLE);*/
// adding movie to movies array
} 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) {
String TAG="";
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(movieReq);
here is logcat error
10-29 08:21:34.953: D/(3631): [{"work":"software","id":76,"username":"admin123","phnum":"90000000000","image_src":"http:\/\/demo.cogzidel.com\/dropinn\/images\/no_avatar.jpg","email":"admin#gmail.com","Fname":"admin","describe":"developer","Lname":"admin123","live":"chennai"}]
10-29 08:21:34.953: D/OUTPUT IS(3631): admin
10-29 08:21:34.953: D/AndroidRuntime(3631): Shutting down VM
10-29 08:21:34.953: W/dalvikvm(3631): threadid=1: thread exiting with uncaught exception (group=0xb0d0cb20)
10-29 08:21:34.953: E/AndroidRuntime(3631): FATAL EXCEPTION: main
10-29 08:21:34.953: E/AndroidRuntime(3631): Process: info.androidhive.customlistviewvolley, PID: 3631
10-29 08:21:34.953: E/AndroidRuntime(3631): java.lang.NullPointerException
10-29 08:21:34.953: E/AndroidRuntime(3631): at com.example.dropinnnow.Editprofile$6.onResponse(Editprofile.java:690)
10-29 08:21:34.953: E/AndroidRuntime(3631): at com.example.dropinnnow.Editprofile$6.onResponse(Editprofile.java:1)
10-29 08:21:34.953: E/AndroidRuntime(3631): at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
10-29 08:21:34.953: E/AndroidRuntime(3631): at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
10-29 08:21:34.953: E/AndroidRuntime(3631): at android.os.Handler.handleCallback(Handler.java:733)
10-29 08:21:34.953: E/AndroidRuntime(3631): at android.os.Handler.dispatchMessage(Handler.java:95)
10-29 08:21:34.953: E/AndroidRuntime(3631): at android.os.Looper.loop(Looper.java:136)
10-29 08:21:34.953: E/AndroidRuntime(3631): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-29 08:21:34.953: E/AndroidRuntime(3631): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 08:21:34.953: E/AndroidRuntime(3631): at java.lang.reflect.Method.invoke(Method.java:515)
10-29 08:21:34.953: E/AndroidRuntime(3631): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-29 08:21:34.953: E/AndroidRuntime(3631): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-29 08:21:34.953: E/AndroidRuntime(3631): at dalvik.system.NativeStart.main(Native Method)
Try this, hope it helps you,
fname.setText(obj.getString("Fname").toString());
lname.setText(obj.getString("Lname").toString());
Related
I don't know why it doesn't work, I think I'm doing it correctly.
Error
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: FATAL EXCEPTION: main
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: Process: com.imaginarv.launcher, PID: 11957
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: java.lang.StackOverflowError
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:383)
10-29 09:22:48.995 11957-11957/com.imaginarv.launcher E/AndroidRuntime: at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:378)
apps.json
This file is stored on assets/apps.json.
I checked it on http://jsonviewer.stack.hu/.
[
{
"name": "Carpeta 1",
"packages": [
"com.mobilendo.app1",
"com.mobilendo.app2"
]
},
{
"name": "Carpeta 2",
"packages": [
"com.mobilendo.app1",
"com.mobilendo.app2"
]
}
]
Functions
Functions to read the JSon from apps.json and parse it.
private ArrayList<FolderModel> getFolders() {
String jsonString = loadJSONFromAsset();
Gson gson = new Gson();
ArrayList<FolderModel> foldersList;
foldersList = gson.fromJson(jsonString, new TypeToken<ArrayList<FolderModel>>(){}.getType());
return foldersList;
}
public String loadJSONFromAsset() {
StringBuilder buf = new StringBuilder();
try {
InputStream json = getAssets().open("apps.json");
BufferedReader in = new BufferedReader(new InputStreamReader(json, "UTF-8"));
String str;
while ((str = in.readLine()) != null) {
buf.append(str);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return buf.toString();
}
FolderModel
public class FolderModel {
#SerializedName("name")
private String name;
#SerializedName("packages")
private ArrayList<String> packages;
#SerializedName("apps")
private ArrayList<AppModel> apps;
#SerializedName("icon")
private Drawable icon;
public FolderModel(String name, ArrayList<String> packages) {
this.name = name;
this.packages = packages;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList<String> getPackages() {
return packages;
}
public void setPackages(ArrayList<String> packages) {
this.packages = packages;
}
public ArrayList<AppModel> getApps() {
return apps;
}
public void setApps(ArrayList<AppModel> apps) {
this.apps = apps;
}
public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
}
AppModel
public class AppModel implements Serializable {
private final Context mContext;
private final ApplicationInfo mInfo;
private String mAppLabel;
private Drawable mIcon;
private boolean mMounted;
private final File mApkFile;
public AppModel(Context context, ApplicationInfo info) {
mContext = context;
mInfo = info;
mApkFile = new File(info.sourceDir);
}
public ApplicationInfo getAppInfo() {
return mInfo;
}
public String getApplicationPackageName() {
return getAppInfo().packageName;
}
public String getLabel() {
return mAppLabel;
}
public Drawable getIcon() {
if (mIcon == null) {
if (mApkFile.exists()) {
mIcon = mInfo.loadIcon(mContext.getPackageManager());
return mIcon;
} else {
mMounted = false;
}
} else if (!mMounted) {
// If the app wasn't mounted but is now mounted, reload
// its icon.
if (mApkFile.exists()) {
mMounted = true;
mIcon = mInfo.loadIcon(mContext.getPackageManager());
return mIcon;
}
} else {
return mIcon;
}
return mContext.getResources().getDrawable(android.R.drawable.sym_def_app_icon);
}
public void loadLabel(Context context) {
if (mAppLabel == null || !mMounted) {
if (!mApkFile.exists()) {
mMounted = false;
mAppLabel = mInfo.packageName;
} else {
mMounted = true;
CharSequence label = mInfo.loadLabel(context.getPackageManager());
mAppLabel = label != null ? label.toString() : mInfo.packageName;
}
}
}
}
Remove the Drawable object from FolderModel and from AppModel and save it as an id or image url.
The Drawables are not serializable clases and it's expensive to keep them in memory.
Try something like this:
JSONArray list;
list = new JSONArray(jsonString);
foldersList = new ArrayList<FolderModel>();
FolderModel f;
for (int i = 1; i < list.length(); ++i) {
f=new FolderModel();
JSONObject obj = list.getJSONObject(i);
f.setName(obj.getString("name").toString());
}
I usually use another way to parse:
FolderModel[] arrayModels = gson.fromJson(jsonString, FolderModel[].class);
I am creating a game where with a button listener, server creates an array of integers and return them into the client.
Here you can see the code from the button listener:
rollDiceButton = (Button) findViewById(R.id.roll_dice);
rollDiceButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
rollDice();
new DiceTask() {
#Override
public void onPostExecute(int[] result) {
tempArray = result;
printDice(tempArray,pDice);
clickableDice(pDice);
}
}.execute(socket);
}
});
The rollDice(); method just sends a String to the server to notify him that he can roll the dice.
The Dice Roll into the server:
while(running) {
String st = input.readLine();
if (st.equals("dice")) {
for (int i = 0; i < diceRolled.length; i++) {
diceRolled[i] = (int) ( 1 + Math.random() * 6);
System.out.print(diceRolled[i] + " ");
}
output.writeObject(diceRolled);
}
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
And the code from my AsyncTask, DiceTask(), is this:
public class DiceTask extends AsyncTask<Socket, Void, int[]> {
private int[] arrayFromServer = new int[8];
public DiceTask() {
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected int[] doInBackground(Socket...params) {
Socket soc = params[0];
ObjectInputStream ois;
try {
ois = new ObjectInputStream(soc.getInputStream());
arrayFromServer = (int[]) (ois.readObject());
} catch (StreamCorruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return arrayFromServer;
}
The first time I am pressing the button I can confirm that the array in the server is the same with the array in my client, but the second time, the array in 8 zeros in my client when I can see the 8 normal dice into the server. What I am doing wrong? Where is my mistake?
LOGCAT:
06-08 16:07:25.070: E/AndroidRuntime(638): FATAL EXCEPTION: main
06-08 16:07:25.070: E/AndroidRuntime(638): java.lang.ArrayIndexOutOfBoundsException: length=6; index=-1
06-08 16:07:25.070: E/AndroidRuntime(638): at com.android.pickominoclient.GameActivity.printDice(GameActivity.java:214)
06-08 16:07:25.070: E/AndroidRuntime(638): at com.android.pickominoclient.GameActivity$2$1.onPostExecute(GameActivity.java:84)
06-08 16:07:25.070: E/AndroidRuntime(638): at com.android.pickominoclient.GameActivity$2$1.onPostExecute(GameActivity.java:1)
06-08 16:07:25.070: E/AndroidRuntime(638): at android.os.AsyncTask.finish(AsyncTask.java:602)
06-08 16:07:25.070: E/AndroidRuntime(638): at android.os.AsyncTask.access$600(AsyncTask.java:156)
06-08 16:07:25.070: E/AndroidRuntime(638): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
06-08 16:07:25.070: E/AndroidRuntime(638): at android.os.Handler.dispatchMessage(Handler.java:99)
06-08 16:07:25.070: E/AndroidRuntime(638): at android.os.Looper.loop(Looper.java:137)
06-08 16:07:25.070: E/AndroidRuntime(638): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-08 16:07:25.070: E/AndroidRuntime(638): at java.lang.reflect.Method.invokeNative(Native Method)
06-08 16:07:25.070: E/AndroidRuntime(638): at java.lang.reflect.Method.invoke(Method.java:511)
06-08 16:07:25.070: E/AndroidRuntime(638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-08 16:07:25.070: E/AndroidRuntime(638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-08 16:07:25.070: E/AndroidRuntime(638): at dalvik.system.NativeStart.main(Native Method)
Edit
PrintDice code:
public void printDice(int[] array1, ImageView[] array2) {
for (int i = 0; i < array1.length; i++) {
array2[i].setImageResource(diceImages[array1[i] - 1]);
array2[i].setTag(array1[i]);
array2[i].setVisibility(View.VISIBLE);
}
}
diceImages array:
private final int[] diceImages = new int[] {R.drawable.dice_one, R.drawable.dice_two, R.drawable.dice_three,
R.drawable.dice_four, R.drawable.dice_five, R.drawable.dice_pico };
I'm trying to get a looper thread to work but despite all attempts, it crashes. I'm not using the HandlerThread class, as I'm doing quite a bit of 802.11 related stuff in the thread itself and posting Runnables with lots of duplicate code doesn't seem to be the right way to go here.
Here the skeleton of the looper thread class:
public class WiFiScanner extends Thread {
Looper mLooper;
Handler mHandler;
#Override
public void run() {
Looper.prepare();
synchronized(this) {
mLooper = Looper.myLooper();
notifyAll();
}
mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
..... // message parsing
}
}
}
/* waits for Looper initialization */
public boolean waitForLooper() {
synchronized(this) {
while(mLooper == null) {
try {
wait();
} catch (InterruptedException e) {}
}
}
return true;
}
}
and the code initializing it in the main activity:
wifiScanner = new WiFiScanner(
... // callback stuff
);
wifiScanner.start();
wifiScanner.waitForLooper();
wifiScanner.initialize();
initialize is a simple function in the WiFiScanner class, posting a message bundle to the message queue:
public void initialize() {
Message msg = mHandler.obtainMessage();
Bundle b = new Bundle();
WiFiMsg msgId = WiFiMsg.INITIALIZE;
b.putSerializable("msgId", msgId);
msg.obj = b;
mHandler.sendMessage(msg);
}
Despite waitForLooper() returning successfully, the call to initialize() causes a null pointer exception. I assume the looper isn't really in a state to have message dispatched into the queue as a static delay of 500ms between waitForLooper() and initialize() circumvents this issue.
Any ideas on how to fix this in a reasonably elegant way?
PS: Now by popular demand, a stack trace:
E/AndroidRuntime( 3178): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
E/AndroidRuntime( 3178): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
E/AndroidRuntime( 3178): at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime( 3178): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
E/AndroidRuntime( 3178): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 3178): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 3178): at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime( 3178): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3178): at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime( 3178): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime( 3178): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime( 3178): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 3178): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 3178): at de.uni_leipzig.informatik.rvs.videodownload.wifi.WiFiScanner.initialize(WiFiScanner.java:204)
E/AndroidRuntime( 3178): at de.uni_leipzig.informatik.rvs.videodownload.MainActivity.onCreate(MainActivity.java:270)
E/AndroidRuntime( 3178): at android.app.Activity.performCreate(Activity.java:5133)
E/AndroidRuntime( 3178): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime( 3178): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
E/AndroidRuntime( 3178): ... 11 more
Your notify should be after mHandler is set, not mLooper - since your NPE is on mHandler.
I recently added a splash screen to my Android application to mask the loading of a JSON file, but my app crashes after finishing parsing. I've commented out and uncommented out code until I found the problematic segment, but I'm not sure why it isn't working.
The code works if I comment out the for loop from this code segment.
From MagazinePagesActivity.java:
public void loadItems() {
for (Item item : SplashActivity.downloadedItems) {
Post post = (Post) item;
for (String tag : post.tags) {
if (tag.equals(this.tag)) {
// loader.loadImage(magazine.imageURL, new
// SimpleImageLoadingListener());
Bundle bundle = new Bundle();
bundle.putString("title", post.title);
bundle.putString("article", post.article);
bundle.putString("imageURL", post.imageURL);
MagazineFragment cover = new MagazineFragment();
cover.setArguments(bundle);
coverAdapter.addNewItem(cover);
}
}
}
setContentView(vp);
}
At first, I thought it was because SplashActivity.downloadedItems wasn't properly defined, but it looks fine to me:
From SplashActivity.java:
public static ArrayList<Item> downloadedItems = new ArrayList<Item>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new RetreiveJSONTask() {
protected void onPostExecute(String JSON)
{
Log.d("SplashActivity", "Beginning parsingJSON" );
downloadedItems = parseJSON ( JSON );
Log.d("SplashActivity", "Finished parsingJSON" );
Log.d("SplashActivity", "" + SplashActivity.downloadedItems.isEmpty());
Intent i = new Intent(SplashActivity.this, MagazinePagesActivity.class);
startActivity(i);
// Closes the splash screen
finish();
}
}.execute(sourceURL());
}
From LogCat:
10-16 19:23:12.602: D/AndroidRuntime(828): Shutting down VM
10-16 19:23:12.602: W/dalvikvm(828): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-16 19:23:12.632: E/AndroidRuntime(828): FATAL EXCEPTION: main
10-16 19:23:12.632: E/AndroidRuntime(828): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bruinfootball/com.dailybruin.bruinframework.channels.StoryListActivity}: java.lang.NullPointerException
10-16 19:23:12.632: E/AndroidRuntime(828): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-16 19:23:12.632: E/AndroidRuntime(828): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-16 19:23:12.632: E/AndroidRuntime(828): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-16 19:23:12.632: E/AndroidRuntime(828): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-16 19:23:12.632: E/AndroidRuntime(828): at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 19:23:12.632: E/AndroidRuntime(828): at android.os.Looper.loop(Looper.java:137)
10-16 19:23:12.632: E/AndroidRuntime(828): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-16 19:23:12.632: E/AndroidRuntime(828): at java.lang.reflect.Method.invokeNative(Native Method)
10-16 19:23:12.632: E/AndroidRuntime(828): at java.lang.reflect.Method.invoke(Method.java:525)
10-16 19:23:12.632: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-16 19:23:12.632: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-16 19:23:12.632: E/AndroidRuntime(828): at dalvik.system.NativeStart.main(Native Method)
10-16 19:23:12.632: E/AndroidRuntime(828): Caused by: java.lang.NullPointerException
10-16 19:23:12.632: E/AndroidRuntime(828): at com.dailybruin.bruinframework.channels.StoryListActivity.loadItems(StoryListActivity.java:27)
10-16 19:23:12.632: E/AndroidRuntime(828): at com.dailybruin.bruinframework.channels.JSONItemsActivity.onCreate(JSONItemsActivity.java:20)
10-16 19:23:12.632: E/AndroidRuntime(828): at com.dailybruin.bruinframework.channels.StoryItemsActivity.onCreate(StoryItemsActivity.java:18)
10-16 19:23:12.632: E/AndroidRuntime(828): at com.dailybruin.bruinframework.channels.StoryListActivity.onCreate(StoryListActivity.java:18)
10-16 19:23:12.632: E/AndroidRuntime(828): at android.app.Activity.performCreate(Activity.java:5133)
10-16 19:23:12.632: E/AndroidRuntime(828): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-16 19:23:12.632: E/AndroidRuntime(828): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-16 19:23:12.632: E/AndroidRuntime(828): ... 11 more
Not sure if it helps, but both SplashActivity and MagazinePagesActivity extend JSONItemsActivity, separately.
Thanks in advance for any help! I'm fairly new to Android, so please bear with me.
Edit: That's odd, the debug statement I put to check if the ArrayList is empty returns true, but another debug statement within parseJSON shows that things are being added. My parseJSON method:
public ArrayList<Item> parseJSON(String jsonString) {
ArrayList<Item> magazines = new ArrayList<Item>();
JSONArray jArray;
try {
jArray = new JSONArray(jsonString);
JSONObject jObject;
JSONObject image;
String img;
String title;
String subtitle;
int commentCount;
String content;
String date;
String ID;
String slug;
JSONArray array;
String[] tags;
for (int i = 0; i < jArray.length(); i++) {
jObject = jArray.getJSONObject(i);
img = jObject.getString("tall_image_url");
if (img.equals(""))
img = jObject.getString("image_url");
if (img.equals("") && !jObject.isNull("poster_image")) {
image = jObject.getJSONObject("poster_image");
img = image.getString("name");
}
title = jObject.getString("title");
subtitle = jObject.getString("subtitle");
commentCount = jObject.getInt("comment_count");
content = jObject.getString("content_html");
date = jObject.getString("creation_date");
ID = jObject.getString("id");
slug = jObject.getString("slug");
array = jObject.getJSONArray("tags_list");
tags = new String[array.length()];
for (int j = 0; j < array.length(); j++) {
tags[j] = array.getString(j);
}
for (String tag : tags) {
if (tag.equals(this.tag))
{
Post additive = new Post(img, title, subtitle,
commentCount, content, date, ID, slug, tags);
magazines.add(additive);
Log.d("SplashActivity", additive.toString());
}
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return magazines;
}
StoryListActivity line 27:
public void loadItems() {
(findViewById(R.id.progress_bar)).setVisibility(View.GONE);
getSupportFragmentManager().beginTransaction()
.replace(R.id.loading, new StoryListFragment()).commit();
}
StoryItemsActivity (MagazinePagesActivity and StoryListActivity extend this class; this class extends JSONItemsActivity, which SplashActivity also extends):
package com.dailybruin.bruinframework.channels;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.dailybruin.bruinframework.R;
import com.dailybruin.bruinframework.base.BaseActivity;
import com.dailybruin.bruinframework.base.URLBuilder;
import android.os.Bundle;
public abstract class StoryItemsActivity extends JSONItemsActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public String sourceURL()
{
return URLBuilder.getPostsURL(getString(R.string.slug));
}
public ArrayList<Item> parseJSON(String jsonString) {
// Using pre-downloaded and parsed JSON
return SplashActivity.downloadedItems;
}
}
Check this part of the code. Line number 27. That is null and hence causing NPE.
Caused by: java.lang.NullPointerException
10-16 19:23:12.632: E/AndroidRuntime(828): at com.dailybruin.bruinframework.channels.StoryListActivity.loadItems(StoryListActivity.java:27)
I am trying to implement a login after is shown progressbar but it shows an erro.Below is the code I am using
class PostService extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
String s = UserFunctions.response;
Log.d(Details.TAG, "checking i response...");
UserFunctions.basicLogin(email, password);
Log.d(Details.TAG,
s + " " + UserFunctions.basicLogin(email, password));
return s;
}
Intent i;
#Override
protected void onPostExecute(String r) {
// TODO Auto-generated method stub
super.onPostExecute(UserFunctions.response);
Log.d(Details.TAG, "Password Point");
if(UserFunctions.basicLogin(email, password)){
i = new Intent("com.example.eve_haps.DASHBOARDACTIVITY");
startActivity(i);
}else {
Toast.makeText(MainActivity.this,
"Login UnSuccesful. Check email and/or password",
Toast.LENGTH_SHORT).show();
}
}
}
But it returns an error log
E/AndroidRuntime( 7465): FATAL EXCEPTION: main
E/AndroidRuntime( 7465): java.lang.StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=17
E/AndroidRuntime( 7465): at java.lang.String.startEndAndLength(String.java:593)
E/AndroidRuntime( 7465): at java.lang.String.substring(String.java:1474)
E/AndroidRuntime( 7465): at com.example.eve_haps.UserFunctions.basicLogin(UserFunctions.java:107)
E/AndroidRuntime( 7465): at com.example.eve_haps.MainActivity$PostService.onPostExecute(MainActivity.java:105)
E/AndroidRuntime( 7465): at com.example.eve_haps.MainActivity$PostService.onPostExecute(MainActivity.java:1)
E/AndroidRuntime( 7465): at android.os.AsyncTask.finish(AsyncTask.java:602)
E/AndroidRuntime( 7465): at android.os.AsyncTask.access$600(AsyncTask.java:156)
E/AndroidRuntime( 7465): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
E/AndroidRuntime( 7465): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 7465): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime( 7465): at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime( 7465): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 7465): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime( 7465): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime( 7465): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime( 7465): at dalvik.system.NativeStart.main(Native Method)
Below is the code for implementing the login
public static boolean basicLogin(String email, String password) {
String msg = "Login successful";
boolean status = false;
String url;
String links[] = {
"..." + "?user=",
"..." + "?actn=login"
+ "&user=" };
auth_link = links[0]+email + "&password=" + password;
response = Connect.doHTTPGet(auth_link);
// result = getHttpResponse(url);
Log.d(Details.TAG, " " + msg.length() + " " + UserFunctions.response.substring(0, 17) + " "+ UserFunctions.response.substring(0, 17).length() );
if (UserFunctions.response.substring(0, 17).trim().equals(msg)) {
Log.d(Details.TAG, " " + msg.length() + " " + UserFunctions.response.substring(0, 17));
status = true;
}
Log.d(Details.TAG, " " + "Connection Test"
+ UserFunctions.response);
return status;
Please what is the error. I would reply appreciate it