Trying to use AsyncTask for logging in user to parse.com before proceeding. Am getting a class cast exception.
Relevant Code:
package com.simoyi.chatabout;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.parse.ParseAnalytics;
import com.parse.ParseUser;
public class GroupsActivity extends Activity {
private static Cursor result;
private static SimpleCursorAdapter adapter;
private Context context;
private ListView lv;
private ProgressDialog prgDialog;
private TextView txt;
private CheckAuthD check;
private TextView b;
/**
* *****************************************************************************************************
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_groups);
b = (TextView) findViewById(R.id.text4);
context = this;
ParseAnalytics.trackAppOpened(getIntent());
checkAuth();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_groups, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* *****************************************************************************************************
*/
public void checkAuth() {
if (ParseUser.getCurrentUser() != null) {
initializeViews();
} else {
AsyncTask load = new checkForAuth();
load.execute();
}
}
public void getLoadResult(String result) {
/*
check.dieDdie();
if(result){
checkAuth();
}
else{
Intent intent = new Intent(this, SignUpActivity.class);
startActivity(intent);
}
*/
check.dieDdie();
TextView a = new TextView(this);
a.setText(result);
setContentView(a);
}
public void initializeViews() {
try {
lv = (ListView) findViewById(R.id.groups);
Utility u = new Utility(context);
result = u.getJoinedGroups();
adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
result,
new String[]{"groupName", "desc"},
new int[]{android.R.id.text1, android.R.id.text2});
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
String selected = ((TextView) view.findViewById(android.R.id.text1)).getText().toString();
Toast toast = Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT);
toast.show();
}
});
} catch (Exception ex) {
Toast toast = Toast.makeText(getApplicationContext(), ex.getMessage(), Toast.LENGTH_SHORT);
toast.show();
}
}
/*
public void showCheckDialoga() {
FragmentManager fm = getSupportFragmentManager();
check = new CheckAuthD();
check.show(fm, "dialog");
txt = (TextView) findViewById(R.id.loading);
}
*/
public void updateDialoga(String update) {
txt.setText(update + "\n" + "Loading...");
}
/**
* ******************************************************************************************************************
*/
private class checkForAuth extends AsyncTask<String, String, String> {
GroupsActivity act;
Context context;
String result;
/*
public checkForAuth(GroupsActivity a){
act = a;
context = a;
}
*/
@Override
protected void onPreExecute() {
//act.showCheckDialoga();
b.setText("Beginning");
}
@Override
protected String doInBackground(String... params) {
try {
Account account = new Account(context);
if (account.checkForUsername()) {
publishProgress("Identified user account...");
account.logIn();
publishProgress("Account logged In...");
result = "yes";
} else {
result = "no";
}
} catch (Exception e) {
e.printStackTrace();
result = e.getMessage();
}
return result;
}
protected void onProgressUpdate(String... progress) {
//act.updateDialoga(progress[0]);
b.setText(progress[0]);
}
@Override
protected void onPostExecute(String result) {
//act.getLoadResult(result);
b.setText(result);
}
}
}
LOGCAT:
--------- beginning of crash
04-19 11:09:42.108 2473-2505/com.simoyi.chatabout E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.simoyi.chatabout, PID: 2473
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]
at com.simoyi.chatabout.checkForAuth.doInBackground(GroupsActivity.java:139)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
What could be the problem?
Aucun commentaire:
Enregistrer un commentaire