UIを実行するMain.javaというクラスがあります。AsyncTaskを拡張するVerifyCreds.javaという別のクラスがあります。VerificationCredsの全体的な目的は、UIがロックされないように、別のスレッドでサーバーへの呼び出しを実行することです。サーバーを呼び出す他のクラスがいくつかあり、それらすべてにVerifyCredsクラスを使用して実行させたいので、Main.javaにサブクラス化されていません。
私ができる必要があるのは、VerifyCreds.javaからMain.javaにデータを戻すことです。しかし、以下のコードでエラーが発生します。
どんな助けでも大歓迎です!ありがとう!
これが私のMain.javaコードです
package com.coolprograms.zeal;
<<<imports removed for brevity>>
public class Main extends Activity
{
// Global VAR's
String TAG = "ZEAL";
static Boolean authCode;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//Grab elements
final EditText userName = (EditText)findViewById(R.id.usernameBOX);
final EditText userPWD = (EditText)findViewById(R.id.passwordBOX);
final Button loginBTN = (Button)findViewById(R.id.loginBTTN);
//Button listener
loginBTN.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// Get username and password and add to array
ArrayList<String> creds = new ArrayList<String>();
creds.add(userName.getText().toString());
creds.add(userPWD.getText().toString());
// Call method that will execute AsyncTask to get creds from server
verify(creds);
}
});
}
private void verify (ArrayList<String> creds)
{
// Start AsyncTask to get creds
new VerifyCredentials(this).execute(creds);
}
public interface credsAuth
{
public void authorizedCreds(Boolean authCode);
}
public void verifiedCreds(Context ctx, Boolean serverAuth)
{
if(serverAuth = true)
{
// Move to next screen if we are authorized
Intent i = new Intent(ctx, Items.class);
startActivity(i);
}
}
}
これがVerifyCreds.javaコードです
package com.coolprograms.zeal;
<<<imports removed for brevity>>
public class VerifyCredentials extends AsyncTask<ArrayList<String>, Void, Boolean> {
private Context ctx;
ProgressDialog dialog;
public VerifyCredentials(Context applicationContext)
{
// TODO Auto-generated constructor stub
ctx = applicationContext;
dialog = new ProgressDialog(applicationContext);
}
@Override
protected void onPreExecute()
{
dialog.setTitle("Please wait");
dialog.setMessage("Verifying username and password...");
dialog.show();
}
@Override
protected Boolean doInBackground(ArrayList<String>...creds)
{
//To return
Boolean serverAnwser = false;
//Get the creds
String userID = creds[0].get(0).toString();
Log.i("ZEAL", "Creds[0]: " + creds[0].get(0).toString());
String userPWD = creds[0].get(1).toString();
Log.i("ZEAL", "Creds[1]: " + creds[0].get(1).toString());
//Get creds from server
try {
String serverANW = null;
URL getCreds = new URL("http://XXX.XXX.XXX.XXX/api/api.php?method=getCreds&id=" + userID + "&pwd=" + userPWD);
Log.i("ZEAL", "Webservice URL: " + getCreds.toString());
URLConnection tc = getCreds.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
tc.getInputStream()));
// Get JSON Object
JSONArray jArray;
jArray = new JSONArray(in.readLine());
//Log json object returned
Log.i("ZEAL", jArray.toString());
//Check server response
//for (int i = 0; i < jArray.length(); i++)
//{
JSONObject e = jArray.getJSONObject(0);
String s = e.getString("RETURN");
JSONObject jObject = new JSONObject(s);
serverANW = jObject.getString("RESULT");
Log.i("ZEAL", "API Result: " + serverANW);
//}
if(serverANW.equalsIgnoreCase("True"))
{
serverAnwser = true;
Log.i("ZEAL", "ServerANW = " + serverANW.toString());
Log.i("ZEAL", "Setting server anwser to true");
}
else
{
serverAnwser = false;
Log.i("ZEAL", "ServerANW = " + serverANW.toString());
Log.i("ZEAL", "Setting server anwser to false - " + serverAnwser.toString());
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return serverAnwser;
}
@Override
protected void onPostExecute(final Boolean authCode)
{
dialog.dismiss();
// Log what the functions is doing
Log.d("ZEAL","Server Response: " + authCode.toString());
Toast.makeText(ctx, "Server says: " + authCode.toString(), Toast.LENGTH_LONG).show();
Main m = new Main();
m.verifiedCreds(m, authCode);
}
}
エラー:
05-23 15:18:57.190: E/AndroidRuntime(25475): FATAL EXCEPTION: main
05-23 15:18:57.190: E/AndroidRuntime(25475): java.lang.NullPointerException
05-23 15:18:57.190: E/AndroidRuntime(25475): at android.content.ContextWrapper.getPackageName(ContextWrapper.java:127)
05-23 15:18:57.190: E/AndroidRuntime(25475): at android.content.ComponentName.<init>(ComponentName.java:75)
05-23 15:18:57.190: E/AndroidRuntime(25475): at android.content.Intent.<init>(Intent.java:3174)
05-23 15:18:57.190: E/AndroidRuntime(25475): at com.coolprograms.zeal.Main.verifiedCreds(Main.java:85)
05-23 15:18:57.190: E/AndroidRuntime(25475): at com.coolprograms.zeal.VerifyCredentials.onPostExecute(VerifyCredentials.java:117)
05-23 15:18:57.190: E/AndroidRuntime(25475): at com.coolprograms.zeal.VerifyCredentials.onPostExecute(VerifyCredentials.java:1)
05-23 15:18:57.190: E/AndroidRuntime(25475): at android.os.AsyncTask.finish(AsyncTask.java:602)
05-23 15:18:57.190: E/AndroidRuntime(25475): at android.os.AsyncTask.access$600(AsyncTask.java:156)
05-23 15:18:57.190: E/AndroidRuntime(25475): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
05-23 15:18:57.190: E/AndroidRuntime(25475): at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 15:18:57.190: E/AndroidRuntime(25475): at android.os.Looper.loop(Looper.java:137)
05-23 15:18:57.190: E/AndroidRuntime(25475): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-23 15:18:57.190: E/AndroidRuntime(25475): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 15:18:57.190: E/AndroidRuntime(25475): at java.lang.reflect.Method.invoke(Method.java:511)
05-23 15:18:57.190: E/AndroidRuntime(25475): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-23 15:18:57.190: E/AndroidRuntime(25475): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-23 15:18:57.190: E/AndroidRuntime(25475): at dalvik.system.NativeStart.main(Native Method)