/ *每当asynctask执行时,应用程序总是崩溃。我在另一个文件中将这个asynctask作为一个独立的程序运行,它完美地工作,所以我不确定为什么它不能与我的代码的其他部分一起使用。* /
public class MainActivity extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
private int a=0;
private int b=0;
private int c=0;
private int d=0;
private int e=0;
private int f=0;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
// url to get all policy list
private static String url_all_products = "http://192.168.1.12/thesis/get_all_policy.php";
String[] policyID = new String[100];
String[] policyname = new String[100];
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_POLICIES = "policylist";
private static final String TAG_PID = "Policy_Status";
private static final String TAG_NAME = "Policy_Name";
//private static final String TAG_STATUS = "Policy_Status";
// products JSONArray
JSONArray product = null;
private final static String LOG_TAG = "DevicePolicyAdmin";
DevicePolicyManager AnguardDevicePolicyManager;
ComponentName AnguardDevicePolicyAdmin;
private CheckBox AnguardAdminEnabledCheckbox;
protected static final int REQUEST_ENABLE = 1;
protected static final int SET_PASSWORD = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_policy_admin);
AnguardDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
AnguardDevicePolicyAdmin = new ComponentName(this,
MyDevicePolicyReceiver.class);
// Get listview
//ListView lv = getListView();
AnguardAdminEnabledCheckbox = (CheckBox) findViewById(R.id.checkBox1);
// Hashmap for ListView
// Loading products in Background Thread
new LoadPolicyList().execute(); //AsyncTask is executed
}
@Override
protected void onResume() {
super.onResume();
if (isMyDevicePolicyReceiverActive()) {
AnguardAdminEnabledCheckbox.setChecked(true);
} else {
AnguardAdminEnabledCheckbox.setChecked(false);
}
AnguardAdminEnabledCheckbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Intent intent = new Intent(
DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(
DevicePolicyManager.EXTRA_DEVICE_ADMIN,
AnguardDevicePolicyAdmin);
intent.putExtra(
DevicePolicyManager.EXTRA_ADD_EXPLANATION,
getString(R.string.admin_explanation));
startActivityForResult(intent, REQUEST_ENABLE);
// new LoadPolicyList().execute();
} else {
AnguardDevicePolicyManager
.removeActiveAdmin( AnguardDevicePolicyAdmin);
}
}
});
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_ENABLE:
Log.v(LOG_TAG, "Enabling Policies Now");
if(a==1)
AnguardDevicePolicyManager.wipeData(1234566);
if(b==1)
AnguardDevicePolicyManager.setMaximumTimeToLock(AnguardDevicePolicyAdmin, 10L);
if(c==1)
AnguardDevicePolicyManager.setMaximumFailedPasswordsForWipe( AnguardDevicePolicyAdmin, 5);
if(d==1)
AnguardDevicePolicyManager.setPasswordQuality(AnguardDevicePolicyAdmin,DevicePolicyManager.PASSWORD_QUALITY_COMPLEX);
if(e==1)
AnguardDevicePolicyManager.setCameraDisabled(AnguardDevicePolicyAdmin, true);
boolean isSufficient = AnguardDevicePolicyManager
.isActivePasswordSufficient();
if (isSufficient) {
AnguardDevicePolicyManager.lockNow();
} else {
Intent setPasswordIntent = new Intent(
DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
startActivityForResult(setPasswordIntent, SET_PASSWORD);
AnguardDevicePolicyManager.setPasswordExpirationTimeout(
AnguardDevicePolicyAdmin, 10000L);
}
break;
}
}
}
class LoadPolicyList extends AsyncTask<String, String, String> {
// I used json in this part to get database information it works fine as a stand alone.
protected String doInBackground(String... args) {
// Building Parameters
//List<NameValuePair> params = new ArrayList<NameValuePair>();
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Policies: ", json.toString());
try{
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Policylist
product = json.getJSONArray(TAG_POLICIES);
// looping through All Policies
for (int i = 0; i < product.length(); i++) {
JSONObject c = product.getJSONObject(i);
// Storing each json item in variable
//String id = c.getString(TAG_PID);
String status = c.getString(TAG_PID);
String name = c.getString(TAG_NAME);
policyID[i] = status;
policyname[i] = name;
Log.d(policyID[i], "This is the Policy's status");
Log.d(policyname[i], "This is the Policy Name");
if(name.equals("Policy1"))
{Log.d(name, "This is it");
Log.d(status, "This is it part 2 HOORAY");
}
}
}
}catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
private boolean isMyDevicePolicyReceiverActive() {
return AnguardDevicePolicyManager
.isAdminActive( AnguardDevicePolicyAdmin);
}
public static class MyDevicePolicyReceiver extends DeviceAdminReceiver {
@Override
public void onDisabled(Context context, Intent intent) {
Toast.makeText(context, "Anguard's Device Admin Disabled",
Toast.LENGTH_SHORT).show();
}
@Override
public void onEnabled(Context context, Intent intent) {
Toast.makeText(context, "Anguard's Device Admin is now enabled",
Toast.LENGTH_SHORT).show();
}
@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
CharSequence disableRequestedSeq = "Requesting to disable Device Admin";
return disableRequestedSeq;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onPasswordChanged(Context context, Intent intent) {
Toast.makeText(context, "Device password is now changed",
Toast.LENGTH_SHORT).show();
DevicePolicyManager localDPM = (DevicePolicyManager) context
.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName localComponent = new ComponentName(context,
MyDevicePolicyReceiver.class);
localDPM.setPasswordExpirationTimeout(localComponent, 0L);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
public void onPasswordExpiring(Context context, Intent intent) {
// This would require API 11 an above
Toast.makeText(
context,
"Anguard's Device password is going to expire, please change to a new password",
Toast.LENGTH_LONG).show();
DevicePolicyManager localDPM = (DevicePolicyManager) context
.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName localComponent = new ComponentName(context,
MyDevicePolicyReceiver.class);
long expr = localDPM.getPasswordExpiration(localComponent);
long delta = expr - System.currentTimeMillis();
boolean expired = delta < 0L;
if (expired) {
localDPM.setPasswordExpirationTimeout(localComponent, 10000L);
Intent passwordChangeIntent = new Intent(
DevicePolicyManager.ACTION_SET_NEW_PASSWORD);
passwordChangeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(passwordChangeIntent);
}
}
@Override
public void onPasswordFailed(Context context, Intent intent) {
Toast.makeText(context, "Password failed", Toast.LENGTH_SHORT)
.show();
}
@Override
public void onPasswordSucceeded(Context context, Intent intent) {
Toast.makeText(context, "Access Granted", Toast.LENGTH_SHORT)
.show();
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i(LOG_TAG,
"MyDevicePolicyReciever Received: " + intent.getAction());
super.onReceive(context, intent);
}
}
}
错误日志
05-26 23:39:53.380 1617-1631/com.example.gregsiy.deviceadministration E/Buffer Error﹕ Error converting result java.lang.NullPointerException: lock == null
05-26 23:39:53.380 1617-1631/com.example.gregsiy.deviceadministration E/JSON Parser﹕ Error parsing data org.json.JSONException: End of input at character 0 of
05-26 23:39:53.390 1617-1631/com.example.gregsiy.deviceadministration W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0xb2c91b20)
05-26 23:39:53.390 1617-1631/com.example.gregsiy.deviceadministration E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.gregsiy.deviceadministration, PID: 1617
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:841)
Caused by: java.lang.NullPointerException
at com.example.gregsiy.deviceadministration.MainActivity$LoadPolicyList.doInBackground(MainActivity.java:165)
at com.example.gregsiy.deviceadministration.MainActivity$LoadPolicyList.doInBackground(MainActivity.java:155)
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)
答案 0 :(得分:0)
java.lang.Thread.run(Thread.java:841)由com.example的com.example.gregsiy.deviceadministration.MainActivity $ LoadPolicyList.doInBackground(MainActivity.java:165)引起的:java.lang.NullPointerException .gregsiy.deviceadministration.MainActivity $ LoadPolicyList.doInBackground(MainActivity.java:155)at android.os.AsyncTask $ 2.call(AsyncTask.java:288)at java.util.concurrent.FutureTask.run(FutureTask.java:237)在
MainActivity
班级中的第165行表示null error
我猜第165行是以下代码之一
if(name.equals("Policy1"))
{Log.d(name, "This is it");
Log.d(status, "This is it part 2 HOORAY");
}
因此,在这种情况下,您应该检查status
或name
变量的值
更新:(我猜name
变量是null
)
希望它有所帮助。谢谢!