2016-06-10 97 views
0

我有应用程序使用PHP添加新的数据库到数据库。如果我在程序中单击按钮创建ID它总是关闭除插入数据库中的数据之外的所有内容。Android应用程序关闭后点击按钮

我不知道如何解决它。在我的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(); 
    } 
} 

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

回答

0

doInBackground不是ui线程的一部分。所以你不能做任何关于UI的事情,因为你的例子开始一个新的屏幕是一个UI工作,但你在回线程中调用它,这会导致崩溃。所以只需将你的startActivity块移动到onPostExecute。另外,finish()会关闭整个屏幕。最后一件事,你的catch块只能捕获JSONException,在你的情况下这还不够。如果您将其更改为Exception,则会在logcat中看到异常日志。

相关问题