无法在onClickListener中调用edittext

时间:2016-06-27 13:55:34

标签: android

我有一个代码来更新数据库中的数据,但是当我想在onClickListener中调用EditText时出现一些问题,在类NetCheck中得到类似http://prntscr.com/blorcg的错误

我的完整代码活动

public class IGNActivity extends AppCompatActivity implements View.OnClickListener{
    Toolbar toolbar;
    private EditText editTextId;
    private Button buttonGet;
    private TextView account, name, race, job, map, level, dalant, gold, cpt, pvp, cash, premi;

    private ProgressDialog pDialog;
    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();
    JSONParser jsonParser = new JSONParser();
    private ListView listView;
    ArrayList<HashMap<String, String>> productsList;

    // url to get all products list
    private static String url_input_item = "http://192.168.1.113/add/update_cash.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCTS = "detail";
    private static final String TAG_ONLINE = "Total Online";
    private static final String TAG_CPT = "PvpPoint";
    private static final String TAG_RACE = "Race";
    final Context context = this;
    // products JSONArray
    JSONArray products = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ign);
        TypedValue typedValueColorPrimaryDark = new TypedValue();

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        IGNActivity.this.getTheme().resolveAttribute(R.attr.colorPrimary, typedValueColorPrimaryDark, true);
        final int colorPrimaryDark = typedValueColorPrimaryDark.data;
        if (Build.VERSION.SDK_INT >= 21) {
            getWindow().setStatusBarColor(colorPrimaryDark);
        }
        editTextId = (EditText) findViewById(R.id.editTextId);
        buttonGet = (Button) findViewById(R.id.buttonGet);
        account = (TextView) findViewById(R.id.accountplayer);
        name = (TextView) findViewById(R.id.ignPlayer);
        race = (TextView) findViewById(R.id.serialplayer);
        job = (TextView) findViewById(R.id.raceplayer);
        map = (TextView) findViewById(R.id.classplayer);
        level = (TextView) findViewById(R.id.lvlplayer);
        dalant = (TextView) findViewById(R.id.dalantplayer);
        gold = (TextView) findViewById(R.id.goldplayer);
        cpt = (TextView) findViewById(R.id.pvpplayer);
        pvp = (TextView) findViewById(R.id.cptplayer);
        cash = (TextView) findViewById(R.id.cc);
        premi = (TextView) findViewById(R.id.premi);

        //buttonGet.setOnClickListener(this);
        /** Update Level **/
        level.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // get prompts.xml view
                LayoutInflater li = LayoutInflater.from(context);
                View promptsView = li.inflate(R.layout.update_level, null);
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);
                // set prompts.xml to alertdialog builder
                alertDialogBuilder.setView(promptsView);
                final EditText userInput = (EditText) promptsView
                        .findViewById(R.id.editTextDialogUserInput);
                userInput.setText(level.getText());
                // set dialog message
                alertDialogBuilder
                        .setCancelable(false)
                        .setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,int id) {
                                        // get user input and set it to result
                                        // edit text
                                        //result.setText(userInput.getText());
                                        new NetCheck().execute();
                                    }
                                })
                        .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,int id) {
                                        dialog.cancel();
                                    }
                                });


                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();
                // show it
                alertDialog.show();
            }
        });
        class NetCheck extends AsyncTask<String, String, String> {

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

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {
            String ign = editTextId.getText().toString().trim();
            String code = userInput.getText().toString();

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("ign", ign));
            params.add(new BasicNameValuePair("code", code));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_input_item,
                    "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();


                    // 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();
            Intent i = new Intent(getApplicationContext(), InputItem.class);
            startActivity(i);
            Toast.makeText(IGNActivity.this, "Success Input Item ", Toast.LENGTH_SHORT).show();
        }
}

任何人都可以帮助我解决我的问题

之前感谢

EDITED LOGCAT

06-27 11:12:03.875    1945-2007/net.dragon_dev.www.dzoneconnect E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #3
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
        at java.util.concurrent.FutureTask.run(FutureTask.java:239)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.NullPointerException
        at net.dragon_dev.www.dzoneconnect.IGNActivity$NetCheck.doInBackground(IGNActivity.java:486)
        at net.dragon_dev.www.dzoneconnect.IGNActivity$NetCheck.doInBackground(IGNActivity.java:453)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask.run(FutureTask.java:234)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
        at java.lang.Thread.run(Thread.java:841)
     06-27 11:12:04.647    1945-1945/net.dragon_dev.www.dzoneconnect E/WindowManager﹕ Activity net.dragon_dev.www.dzoneconnect.IGNActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{52eabd28 V.E..... R......D 0,0-684,192} that was originally added here
android.view.WindowLeaked: Activity net.dragon_dev.www.dzoneconnect.IGNActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{52eabd28 V.E..... R......D 0,0-684,192} that was originally added here
        at android.view.ViewRootImpl.<init>(ViewRootImpl.java:345)
        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:239)
        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
        at android.app.Dialog.show(Dialog.java:281)
        at net.dragon_dev.www.dzoneconnect.IGNActivity$NetCheck.onPreExecute(IGNActivity.java:465)
        at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
        at android.os.AsyncTask.execute(AsyncTask.java:534)
        at net.dragon_dev.www.dzoneconnect.IGNActivity$1$2.onClick(IGNActivity.java:126)
        at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:153)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

userInput.getText().toString()作为AsyncTask中的参数传递。

String input = userInput.getText().toString();
new NetCheck().execute(input);

在您的AsyncTask

protected String doInBackground(String... args) {
    String ign = editTextId.getText().toString().trim();
    String code = args[0];
    // Your code...
}