簡體   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