繁体   English   中英

如何从“尝试并捕获”中删除代码?

[英]How to remove code from Try and Catch?

我的代码中有在try / catch中的字符串。 我需要访问同一类其他部分中的字符串,但是我不能。 我尝试删除try / catch,并且在没有抛出以下错误的情况下也不允许我这样做:

未处理的异常类型JSONException

这是我的尝试/捕获代码。 使它进入普通类的最佳方法是什么,以便我可以访问同一类中代码其他部分的字符串? (此代码在默认的oncreate捆绑包内)

      try {
            // Getting JSON Array
            user = json.getJSONArray(TAG_USER);
            JSONObject c = user.getJSONObject(0);

            // Storing  JSON item in a String Variable

            String name = c.getString(TAG_NAME);
            String messages = c.getString(TAG_MESSAGES);
            String wins = c.getString(TAG_WINS);
            String display = c.getString(TAG_DISPLAY);
            String email = c.getString(TAG_EMAIL);
            String pw = c.getString(TAG_PW);
            String created = c.getString(TAG_CREATED);
            String updated = c.getString(TAG_UPDATED);

            //Importing TextView

            TextView name1 = (TextView)findViewById(R.id.tvfullname);
            TextView messages1 = (TextView)findViewById(R.id.envelope);
            TextView wins1 = (TextView)findViewById(R.id.wins);
            TextView created1 = (TextView)findViewById(R.id.tvcreated_at);
            TextView updated1 = (TextView)findViewById(R.id.tvupdated_at);

            //Set JSON Data in its respectable TextView

            name1.setText("Hello " + name);

            updated1.setText("Your last login was " + updated);

           // print error if applicable.
         } catch (JSONException e) {
               e.printStackTrace();
         }

将您要访问的字符串设置为全局字符串。在类级别声明这些字符串,以便可以在类中的任何位置对其进行访问。

例如,假设您的类名称为X,而您想访问String name则可以这样

class X
{
String name;
try
{
name=c.getString(TAG_NAME);
//other codes
}
catch (JSONException e) {
        e.printStackTrace();
    }
}

现在要回答这个问题, 我需要访问相同类的其他部分中的字符串

您可以使用相同方法或不同方法访问类中的任何位置

在try-catch块外声明字符串

并在try块内为其分配值。

外部尝试块

    String name = null;
    String messages = null;
    String wins = null;
    String display = null;
    String email = null;
    String pw = null;
    String created = null;
    String updated = null;

内试块

// Storing  JSON item in a String Variable

            name = c.getString(TAG_NAME);
            messages = c.getString(TAG_MESSAGES);
            wins = c.getString(TAG_WINS);
            display = c.getString(TAG_DISPLAY);
            email = c.getString(TAG_EMAIL);
            pw = c.getString(TAG_PW);
            created = c.getString(TAG_CREATED);
            updated = c.getString(TAG_UPDATED);

另一种方法,如果要删除try-cath,则是将“ throws”添加到您的方法/类中(以这种方式将异常传播到调用处理类中)。

顺便说一句,这个问题仅在您对Java很陌生的情况下才有意义,否则您将不得不花费大量时间来学习基本的Java课程:)

在尝试之外声明字符串,它们将具有更广泛的范围,以便可以在其他地方使用。

您需要做的是在try-catch之外声明这些变量,可以像在全局一样声明它们

String name;
            String messages;
            String wins ;
            String display;
            String email;
            String pw;
            String created ;
            String updated ;
try {
            // Getting JSON Array

            user = json.getJSONArray(TAG_USER);
            JSONObject c = user.getJSONObject(0);






            // Storing  JSON item in a String Variable

            name = c.getString(TAG_NAME);
            messages = c.getString(TAG_MESSAGES);
            wins = c.getString(TAG_WINS);
            display = c.getString(TAG_DISPLAY);
            email = c.getString(TAG_EMAIL);
            pw = c.getString(TAG_PW);
            created = c.getString(TAG_CREATED);
            updated = c.getString(TAG_UPDATED);



            //Importing TextView

            TextView name1 = (TextView)findViewById(R.id.tvfullname);
            TextView messages1 = (TextView)findViewById(R.id.envelope);
            TextView wins1 = (TextView)findViewById(R.id.wins);
            TextView created1 = (TextView)findViewById(R.id.tvcreated_at);
            TextView updated1 = (TextView)findViewById(R.id.tvupdated_at);




            //Set JSON Data in its respectable TextView

      name1.setText("Hello " + name);





      updated1.setText("Your last login was " + updated);






 // print error if applicable.
    } catch (JSONException e) {
        e.printStackTrace();
    }




    }

将字符串变量声明为Globel。

私有静态最终名称“ NAME”;

在所有必填区域中使用NAME

尝试这个

        String name = null;
        String messages = null;
        String wins = null;
        String display = null;
        String email = null;
        String pw = null;
        String created = null;
        String updated = null;
      try {
        user = json.getJSONArray(TAG_USER);
        JSONObject c = user.getJSONObject(0);

        // Storing  JSON item in a String Variable
        name = c.getString(TAG_NAME);
        messages = c.getString(TAG_MESSAGES);
        wins = c.getString(TAG_WINS);
        display = c.getString(TAG_DISPLAY);
        email = c.getString(TAG_EMAIL);
        pw = c.getString(TAG_PW);
        created = c.getString(TAG_CREATED);
        updated = c.getString(TAG_UPDATED);

        //Importing TextView
        TextView name1 = (TextView)findViewById(R.id.tvfullname);
        TextView messages1 = (TextView)findViewById(R.id.envelope);
        TextView wins1 = (TextView)findViewById(R.id.wins);
        TextView created1 = (TextView)findViewById(R.id.tvcreated_at);
        TextView updated1 = (TextView)findViewById(R.id.tvupdated_at);

        //Set JSON Data in its respectable TextView
        name1.setText("Hello " + name);
        updated1.setText("Your last login was " + updated);

       // print error if applicable.
      } catch (JSONException e) {
               e.printStackTrace();
         }
    }

我认为您可以将getXXX方法替换为optXXX方法,这将是一个更好的选择,您认为呢? 例如:

try {
    name = c.getString(TAG_NAME);
} catch (Exception e) {
    //
}

现在您可以尝试以下代码:

  name = c.optString(TAG_NAME);

好的,仅此而已。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM