无法从Android的EditText字段获取输入

时间:2014-04-04 06:37:55

标签: java android

我正在开发一个Android应用程序。我希望将edittext字段的输入作为字符串。在这里,我想将此字符串用于urlencoder。因此,每当我在文本字段中输入某些内容时,此更改必须转到urlencoder。

    public class MainActivity extends Activity {

int converId=1;
Button button1;
EditText edittext;
String ask, query;{
try {

    ///I want to put user input value at query string as string 

query="hi";

    ///This query string encoded here

ask=URLEncoder.encode(query, "UTF-8");
Log.d("TEST", ask);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} 
}
TextView tv;
String str_botsay;

String fm = "&format=json&convo_id=";
String fpart = "http://10.0.2.2/POM/chatbot/conversation_start.php?say=",
url = fpart+ask+fm+converId;

String URL = url.toString();
public static final String TAG_USERSAY = "usersay";
public static final String TAG_BOTSAY = "botsay";
public static final String TAG_CONVO_ID = "convo_id";

JSONParser jParser = new JSONParser();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv= (TextView) findViewById(R.id.tv);
    edittext= (EditText) findViewById(R.id.editText1);
    button1 = (Button) findViewById(R.id.button1);
    query= edittext.getText().toString();
    button1.setOnClickListener(new View.OnClickListener() {
     public void onClick(View arg0) {

      //I place this line for that but i don't works

    query= edittext.getText().toString();
    new GetBotResponse().execute();
     }
     });

}


private class GetBotResponse extends AsyncTask<Void, Void, Void>{


    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        JSONObject json = jParser.getJSONFromUrlByGet(URL);
        try {

            String str_usersay = json.getString(TAG_USERSAY);

            Log.i("TAG_usersay", str_usersay);
             str_botsay = json.getString(TAG_BOTSAY);

            Log.i("TAG_botsay", str_botsay);
            String str_convo_id = json.getString(TAG_CONVO_ID);

            Log.i("TAG_convo_id", str_convo_id);
        } catch (JSONException e) {
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        tv.setText(str_botsay);

    }

}
    }

请帮我解决这个问题!


这是我项目的日志


04-05 16:00:13.415: D/AndroidRuntime(339): Shutting down VM
04-05 16:00:13.415: W/dalvikvm(339): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-05 16:00:13.515: E/AndroidRuntime(339): FATAL EXCEPTION: main
04-05 16:00:13.515: E/AndroidRuntime(339): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.jsontest/com.example.jsontest.MainActivity}: java.lang.NullPointerException
04-05 16:00:13.515: E/AndroidRuntime(339):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
04-05 16:00:13.515: E/AndroidRuntime(339):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-05 16:00:13.515: E/AndroidRuntime(339):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-05 16:00:13.515: E/AndroidRuntime(339):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-05 16:00:13.515: E/AndroidRuntime(339):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 16:00:13.515: E/AndroidRuntime(339):  at android.os.Looper.loop(Looper.java:123)
04-05 16:00:13.515: E/AndroidRuntime(339):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-05 16:00:13.515: E/AndroidRuntime(339):  at java.lang.reflect.Method.invokeNative(Native Method)
04-05 16:00:13.515: E/AndroidRuntime(339):  at java.lang.reflect.Method.invoke(Method.java:521)
04-05 16:00:13.515: E/AndroidRuntime(339):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-05 16:00:13.515: E/AndroidRuntime(339):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-05 16:00:13.515: E/AndroidRuntime(339):  at dalvik.system.NativeStart.main(Native Method)
04-05 16:00:13.515: E/AndroidRuntime(339): Caused by: java.lang.NullPointerException
04-05 16:00:13.515: E/AndroidRuntime(339):  at java.net.URLEncoder.encode(URLEncoder.java:94)
04-05 16:00:13.515: E/AndroidRuntime(339):  at com.example.jsontest.MainActivity.<init>(MainActivity.java:32)
04-05 16:00:13.515: E/AndroidRuntime(339):  at java.lang.Class.newInstanceImpl(Native Method)
04-05 16:00:13.515: E/AndroidRuntime(339):  at java.lang.Class.newInstance(Class.java:1429)
04-05 16:00:13.515: E/AndroidRuntime(339):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-05 16:00:13.515: E/AndroidRuntime(339):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
04-05 16:00:13.515: E/AndroidRuntime(339):  ... 11 more
04-05 16:05:13.608: I/Process(339): Sending signal. PID: 339 SIG: 9

1 个答案:

答案 0 :(得分:0)

更改按钮点击实施,如下所示

   button1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View arg0) {
    query= edittext.getText().toString();
    url = fpart+query+fm+converId;  
    new GetBotResponse().execute(url);
    }
    });

更改您的AsyncTask,如下面的代码

private class GetBotResponse extends AsyncTask<String, Void, JSONObject>{


@Override
protected JSONObject doInBackground(String... params) {
    // TODO Auto-generated method stub
    JSONObject json = jParser.getJSONFromUrlByGet(params[0]);

    return json;
}

@Override
protected void onPostExecute(JSONObject json) {
if(json!=null)
{
   try {

        String str_usersay = json.getString(TAG_USERSAY);

        Log.i("TAG_usersay", str_usersay);
        String str_botsay = json.getString(TAG_BOTSAY);

        Log.i("TAG_botsay", str_botsay);
        String str_convo_id = json.getString(TAG_CONVO_ID);

        Log.i("TAG_convo_id", str_convo_id);
    } catch (JSONException e) {
    }
   }
    tv.setText(str_botsay);

    }

   }