但是 android.app.Activity 被导入到类中这是我的问题
我必须重新排列代码,以便在仪表板活动(这是我的启动器和主要活动)中发生对必要 API 的所有调用我这样做的原因是因为我需要与该网站建立开放连接,并且我可以从该站点上的不同 api 中检索各种信息(模拟浏览器如何执行此操作)。我发现当我在应用程序中启动和停止活动时,当活动关闭时,与网络的连接也会关闭。因此,每次尝试获取其他信息时,我都需要登录。我在应用程序本身中有很多活动。这由仪表板(一个选项卡活动)控制。以前的代码(工作版本在 GITHUB 上https://github.com/janbenson/agentpitstop),这是目前没有工作的代码
1. Main Activity (dashboard)
package com.jbsoft.android;
...
public class DashboardActivity extends TabActivity {
}
// Check login status in database
userFunctions = new UserFunctions();
String logged_in = userFunctions.isUserLoggedIn( );
if (logged_in == "FALSE") {
// user is not logged in show login screen
Intent login = new Intent(this, LoginActivity.class);
this is line that it hangs up on ---------> startActivity(login);
UserFunctions userFunction = new UserFunctions();
2.登录活动
package com.jbsoft.android;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;
import com.jbsoft.library.UserFunctions;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LoginActivity extends Activity {
Button btnLogin;
// Button btnLinkToRegister;
EditText inputUsername;
EditText inputPassword;
TextView loginErrorMsg;
private static String loginURL = "http://api.agentpitstop.com/mobile/authenticate.php";
private static String root = Environment.getExternalStorageDirectory().toString();
// JSON Response node names
private static String KEY_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.apslogin);
setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.vb1);
// Importing all assets like buttons, text fields
inputUsername = (EditText) findViewById(R.id.loginUsername);
inputPassword = (EditText) findViewById(R.id.loginPassword);
btnLogin = (Button) findViewById(R.id.btnLogin);
// btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
loginErrorMsg = (TextView) findViewById(R.id.login_error);
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = (inputUsername.getText().toString()).trim();
String password = inputPassword.getText().toString();
String temp = username;
temp = temp.replaceAll(" ", "%20");
username = temp;
String parmaction = "?action=login";
String parmusername = "&username=";
parmusername = parmusername + username;
String parmpassword = "&password=";
parmpassword = parmpassword + password;
String parms = parmaction + parmusername + parmpassword;
loginURL = loginURL + parms;
GlobalVariable apploginurl = ((GlobalVariable)getApplicationContext());
apploginurl.setState(loginURL);
finish();
//UserFunctions userFunction = new UserFunctions();
/* JSONObject json = userFunction.loginUser(username, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) == "true") {
userFunction.writeToFile(loginURL);
apploginurl.setBaduser(false);
finish();
} else{
apploginurl.setBaduser(true);
File checkdir = new File (root + "/apsdata") ;
userFunction.DeleteRecursive(checkdir);
finish();
}
} catch (JSONException e) {
e.printStackTrace();
}*/
}
});
/* btnLinkToRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(i);
finish();
}
});*/
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
}
}
最后是我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jbsoft.android"
android:versionCode="1"
android:versionName="1.0" >
<activity
android:label="@string/app_name"
android:name=".DashboardActivity" android:launchMode="standard">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Login Activity -->
<activity
android:label="Login Account"
android:name="com.jbsoft.android.LoginActivity">
</activity>
</application>
</manifest>
这是调试运行的 logcat .....
09-10 13:23:22.433: W/dalvikvm(4689): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
09-10 13:23:22.589: E/AndroidRuntime(4689): FATAL EXCEPTION: main
09-10 13:23:22.589: E/AndroidRuntime(4689): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jbsoft.android/com.jbsoft.android.DashboardActivity}: java.lang.NullPointerException
09-10 13:23:22.589: E/AndroidRuntime(4689): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-10 13:23:22.589: E/AndroidRuntime(4689): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-10 13:23:22.589: E/AndroidRuntime(4689): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-10 13:23:22.589: E/AndroidRuntime(4689): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-10 13:23:22.589: E/AndroidRuntime(4689): at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 13:23:22.589: E/AndroidRuntime(4689): at android.os.Looper.loop(Looper.java:123)