Android 在try中设置文本NullPointerException

Android 在try中设置文本NullPointerException,android,json,nullpointerexception,textview,Android,Json,Nullpointerexception,Textview,我遇到了一个奇怪的问题。 我有一个json对象,它是从internet打包的 此json不为空且json.getString(“非_状态”)==1, 当程序试图执行active.setText(json.getString(“not_status”))时,我得到了NullPointerExeption//它在第92行 如果我更改active.setText(json.getString(“not_status”)至活动.setText(“abc”)并将其移到外部尝试{}阻止它工作正常。问题是-我

我遇到了一个奇怪的问题。 我有一个json对象,它是从internet打包的 此json不为空且
json.getString(“非_状态”)==1
, 当程序试图执行
active.setText(json.getString(“not_status”))时,我得到了
NullPointerExeption
//它在第92行 如果我更改
active.setText(json.getString(“not_status”)
活动.setText(“abc”)
并将其移到外部
尝试{}
阻止它工作正常。问题是-我可以在
内设置文本并尝试{}

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(“Gues:”,“5_1”+json.getString(“not_status”);给我以下信息,证明json.getString(“非_状态”)==1

日志

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;

    }
publicJSONObject getJSONFromUrl(字符串url,列表参数){
//发出HTTP请求
试一试{
//defaultHttpClient
字符串u=url;
u=u+“?”;
//System.setProperty(“192.168.4.200”、“3128”);
//if(httpClient==null){
//httpClient=新的DefaultHttpClient();
//Log.d(“新客户”、“坏客户”);
//          }
httpClient=HttpClientFactory.getThreadSafeClient();
//DefaultHttpClient httpClient=c.getClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));

对于(int i=0;i试试这个。在使用它之前,必须初始化活动文本视图

active = (TextView)findViewById(R.id.yourTextViewname); 
active.setText(json.getString("not_status"));

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

这:

TextView active = (TextView) findViewById(R.id.tv_atc_active);
仅当视图当前在屏幕上时才有效(即,您对包含该ID的xml调用了
setContentView
,或者手动添加了该ID)


因此,很可能是您的
active
为空,然后任何使用它的调用都将抛出
nullpointerException

堆栈跟踪请….TextView active=…;这将是有趣的部分。如果您的Log.d未获得npe,则active必须为空。您是否正确链接了文本视图。如果您的观察结果正确,则它是空的相对安全的结论是,
active
null
。AddToCheckActivity.java文件中第92行是什么?我写了TextView active=…只是为了简短,改为实际的声明尝试这样设置文本,plz让我知道它是如何工作的…active.setText(json.getString(“not_status”)+“”);它已经被一些人提出,然后被删除。只是因为它没有做出任何努力,我再次尝试,但仍然不起作用。尝试将textview声明为全局…我预测内部trycatch无法访问textview…一个c如何工作???1.声明为全局,2.使用findviewbyId在内部装载oncreate,3.将文本设置为inside try catch…..试试这个,希望它能起作用当我删除setText部分时,我没有例外如果
active
为null,那么它的逻辑不是吗?你正在做
null。不管是什么
这是创建
null点异常的原型,关键是:你做了
setContentView
,这个id在xml中吗您没有?setContentView设置正确,tv_atc_active是xml active中声明的textview的id。setText(“abc”)在try{}之外工作
active = (TextView)findViewById(R.id.yourTextViewname); 
active.setText(json.getString("not_status"));
TextView active = (TextView) findViewById(R.id.tv_atc_active);