0

我遇到了 setText 的奇怪问题。我有来自互联网的 json 对象这个 json 不是 null 并且,当程序尝试执行时json.getString("not_status") == 1,我得到了// 如果我更改为并将其移到块外,它就在第 92 行,它工作正常。问题是-我可以在里面设置Sext吗?NullPointerExeptionactive.setText(json.getString("not_status"));active.setText(json.getString("not_status"));active.setText("abc");try{}try{}

JSONObject json = null;
TextView active = (TextView) findViewById(R.id.tv_atc_active);
// some code to put data to json
try {
if (json!=null) {
Log.d("Gues: ", "5_1 " +json.getString("not_status"));
active.setText(json.getString("not_status"));
}
}
catch (JSONExaption e){
e.printTrace
}

Log.d("猜测:", "5_1" +json.getString("not_status")); 给我后续,证明 json.getString("not_status") == 1

04-16 08:50:06.575: D/Gues:(1189): 5_1 1

日志

04-16 08:33:12.944: E/AndroidRuntime(1018): FATAL EXCEPTION: main
04-16 08:33:12.944: E/AndroidRuntime(1018): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.regtest/com.example.regtest.AddToCheckActivity}: java.lang.NullPointerException
04-16 08:33:12.944: E/AndroidRuntime(1018):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-16 08:33:12.944: E/AndroidRuntime(1018):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-16 08:33:12.944: E/AndroidRuntime(1018):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-16 08:33:12.944: E/AndroidRuntime(1018):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-16 08:33:12.944: E/AndroidRuntime(1018):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 08:33:12.944: E/AndroidRuntime(1018):     at android.os.Looper.loop(Looper.java:137)
04-16 08:33:12.944: E/AndroidRuntime(1018):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-16 08:33:12.944: E/AndroidRuntime(1018):     at java.lang.reflect.Method.invokeNative(Native Method)
04-16 08:33:12.944: E/AndroidRuntime(1018):     at java.lang.reflect.Method.invoke(Method.java:511)
04-16 08:33:12.944: E/AndroidRuntime(1018):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-16 08:33:12.944: E/AndroidRuntime(1018):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-16 08:33:12.944: E/AndroidRuntime(1018):     at dalvik.system.NativeStart.main(Native Method)
04-16 08:33:12.944: E/AndroidRuntime(1018): Caused by: java.lang.NullPointerException
04-16 08:33:12.944: E/AndroidRuntime(1018):     at com.example.regtest.AddToCheckActivity.onCreate(AddToCheckActivity.java:92)
04-16 08:33:12.944: E/AndroidRuntime(1018):     at android.app.Activity.performCreate(Activity.java:5008)
04-16 08:33:12.944: E/AndroidRuntime(1018):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-16 08:33:12.944: E/AndroidRuntime(1018):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-16 08:33:12.944: E/AndroidRuntime(1018):     ... 11 more

json输出

04-16 08:50:06.554: D/JSON(1189): {"not_id":"7217801","not_section":"1973","not_street":"m","not_sqTotal":"0","not_sqLiving":"0","not_sqKitchen":"0","not_sqLand":"0","not_unitLand":"0","not_sqHouse":"0","not_flat":"3","not_floor":"0","not_floors":"0","not_title":"\u041f\u0440\u043e\u0434\u0430\u0436\u0430 3 \u043a\u043e\u043c\u043d\u0430\u0442\u043d\u043e\u0439 \u043a\u0432\u0430\u0440\u0442\u0438\u0440\u044b \u0432 \u043d\u043e\u0432\u043e\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445 \u0411\u0435\u043b\u0430\u044f \u0426\u0435\u0440\u043a\u043e\u0432\u044c \u0413\u043b\u0443\u0448\u043a\u0438","not_text":"","user_phone1":"+38111111111","user_phone2":"","user_contact":" ","not_region":"1","not_district":"2","not_settle":"34","not_price":"99999999","not_photo":"a:1:{i:0;a:2:{s:2:\"id\";s:7:\"8036442\";s:4:\"desc\";s:0:\"\";}}","not_date":"2013-04-15 10:46:26","not_date_till":"2013-05-15 00:00:00","not_up":"0","not_premium":"0","not_status":"1","not_huurl":"http:\/\/mirkvartir.ua\/offers\/view\/7217801-prodazha-3-komnatnoj-kvartiry-v-novostrojkax-belaya-cerkov-glushki.html","region_title":"\u041a\u0438\u0435\u0432\u0441\u043a\u0430\u044f","district_title":"\u0411\u0435\u043b\u0430\u044f \u0426\u0435\u0440\u043a\u043e\u0432\u044c","settle_title":"\u0413\u043b\u0443\u0448\u043a\u0438","section_title":"\u041f\u0440\u043e\u0434\u0430\u0436\u0430 \u043a\u0432\u0430\u0440\u0442\u0438\u0440 \u0432 \u043d\u043e\u0432\u043e\u0441\u0442\u0440\u043e\u0439\u043a\u0430\u0445"}

json代码

    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

        // Making HTTP request
        try {
            // defaultHttpClient

            String u = url;
            u = u +"?";
            //System.setProperty("192.168.4.200", "3128");

//          if (httpClient == null) {
//              httpClient = new DefaultHttpClient();
//              Log.d("New Client","bad");
//          }
            httpClient = HttpClientFactory.getThreadSafeClient();
            //DefaultHttpClient httpClient = c.getClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            for(int i =0;i<params.size();i++)
            {
            u =u +params.get(i).getName()+"="+params.get(i).getValue() +"&";
            }
            Log.d("your url is",u);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
            Log.d("data is sent", "true");

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();

        }
        Log.d("wait", "true");
        try {


            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {

                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            Log.d("JSON", json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
4

3 回答 3

0

试试这个。您必须在使用之前初始化活动的 textview。

active = (TextView)findViewById(R.id.yourTextViewname); 
active.setText(json.getString("not_status"));
于 2013-04-16T08:30:23.823 回答
0

您应该使用 findViewById(R.id.TextViewname); 将视图安装到 java;然后执行设置文本。由于您捕获只会捕获 JSONException,因为您只提到了这一点。尝试安装视图,它应该可以正常工作。

于 2013-04-16T08:35:51.570 回答
0

这:

TextView active = (TextView) findViewById(R.id.tv_atc_active);

仅当视图当前在屏幕上时才有效(也就是说,您调用setContentView了包含该 ID 的 xml,或者您手动添加了它。

所以它很可能是你的activeis NULL,然后任何使用它的调用都会抛出一个nullpointerException

于 2013-04-16T08:35:59.053 回答