【问题标题】:How to remove code from Try and Catch?如何从 Try and Catch 中删除代码?
【发布时间】:2013-12-10 04:07:30
【问题描述】:

我的代码中有位于 try/catch 中的字符串。我需要访问同一类其他部分的字符串,但我不能。我尝试删除 try/catch,但它不会让我不抛出以下错误:

未处理的异常类型 JSONException

这是我的 try/catch 代码。将其放入普通类的最佳方法是什么,以便我可以访问同一类中其他代码部分的字符串? (此代码在默认的 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();
         }

【问题讨论】:

    标签: java android json try-catch


    【解决方案1】:

    使您想要访问的那些字符串成为全局的。在类级别声明它们,以便可以在类中的任何位置访问它。

    例如 假设您的班级名称是 X 并且您想访问 String name 然后这样做

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

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

    你可以访问类中的任何地方,无论是相同的方法还是不同的方法

    【讨论】:

    • 我试过了,但在捕获后我仍然无法访问“messages1”。我试图在“if”语句内的公共 void View 视图中调用字符串,但是当我将鼠标悬停在它上面时,它说“messages1 无法解析为变量” :( 我在这里疯了 – user3053446 11 分钟前
    • @user3053446 你在课堂上声明过message1吗?
    • @user3053446 随意询问您是否还有其他疑问
    【解决方案2】:

    在 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;
    

    try 块内

    // 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,另一种方法是向您的方法/类添加“抛出”(这5 种方式您会将异常传播到调用处理类)。

    顺便说一句,这个问题只有当你对java很陌生时才有意义,否则你需要刷你的基本java学校:)

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        你需要做的是在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();
            }
        
        
        
        
            }
        

        【讨论】:

          【解决方案5】:

          将您的字符串变量声明为globel。

          private static final NAME="name";

          在所有需要的区域使用 NAME

          【讨论】:

            【解决方案6】:

            试试这个

                    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();
                     }
                }
            

            【讨论】:

            • 我试过这个,但在捕获后我仍然无法访问“messages1”。我试图在“if”语句内的公共 void View 视图中调用该字符串,但是当我将鼠标悬停在它上面时,它说“messages1 无法解析为变量”:(我在这里发疯了
            【解决方案7】:

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

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

            现在你可以试试下面的代码:

              name = c.optString(TAG_NAME);
            

            好的,就是这样。

            【讨论】:

              猜你喜欢
              • 2020-09-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-03-09
              相关资源
              最近更新 更多