单击按钮后关闭Android应用程序

时间:2016-06-10 18:05:41

标签: php android

我有应用程序使用php向数据库添加新的id。如果我在我的程序中单击按钮 create id ,它总是关闭除数据库中插入的数据之外的所有内容。

我不知道如何修复它。在我的logcat中,没有关于任何错误的信息。

这是我的代码:

Toolbar toolbar;
// Progress Dialog
private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();
EditText inputusername;
EditText inputpassword;
EditText inputnamegm;
TextView registerErrorMsg;
Button createidgm;
// url to create news
private static String url_create_idgm = "http://192.168.1.111/add/create_idgm.php";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_ERROR = "error";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_idgm);

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    TypedValue typedValueColorPrimaryDark = new TypedValue();
    CreateIDGM.this.getTheme().resolveAttribute(R.attr.colorPrimary, typedValueColorPrimaryDark, true);
    final int colorPrimaryDark = typedValueColorPrimaryDark.data;
    if (Build.VERSION.SDK_INT >= 21) {
        getWindow().setStatusBarColor(colorPrimaryDark);
    }

    // Edit Text
    inputusername = (EditText) findViewById(R.id.inputusername);
    inputpassword = (EditText) findViewById(R.id.inputpassword);
    inputnamegm = (EditText) findViewById(R.id.inputname);
    registerErrorMsg = (TextView) findViewById(R.id.error);
    // Create button
    createidgm = (Button) findViewById(R.id.btnCreate);

    // button click event
    createidgm.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // creating new product in background thread
            new NetCheck().execute();
        }
    });
}

/**
 * Background Async Task to Create new product
 * */
class NetCheck extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(CreateIDGM.this);
        pDialog.setMessage("Creating ID Game Master..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {
        String name = inputusername.getText().toString();
        String version = inputpassword.getText().toString();
        String description = inputnamegm.getText().toString();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("username", name));
        params.add(new BasicNameValuePair("password", version));
        params.add(new BasicNameValuePair("name", description));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_create_idgm,
                "POST", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product
                //Toast.makeText(CreateIDGM.this, "Success Create ID Game Master ", Toast.LENGTH_SHORT).show();
                Intent i = new Intent(getApplicationContext(), ListTopGM.class);
                startActivity(i);

                // closing this screen
                finish();
            } else {
                // failed to create product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();
    }
}

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

doInBackground 不是ui线程的一部分。因此你无法对ui做任何事情,因为你的例子开始一个新的屏幕是一个ui工作,但你在反向线程中调用它,这会导致崩溃。因此,只需将startActivity块移动到onPostExecute即可。此外,finish()会关闭整个屏幕。最后一件事,你的catch块只捕获JSONExceptions,这在你的情况下是不够的。如果将其更改为Exception,那么您将在logcat中看到异常日志。