【问题标题】:String not accessible in same class同一类中无法访问字符串
【发布时间】:2013-12-10 01:31:31
【问题描述】:

我有显示祝酒词的代码:

public void checkchallenge(View v) {
        String algo = null;
        if(algo == "123")
        {

Context context = getApplicationContext();
    CharSequence text = "You have " + messages ;
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, text, duration);
    toast.show();
...

我得到的eclipse错误是:

消息无法解析为变量

变量“messages”在上面的一些代码中被调用:

String messages = c.getString(TAG_MESSAGES);
final TextView messages1 = (TextView)findViewById(R.id.envelope);

也许是因为我不太了解 Java,但为什么我的字符串变量“messages”或“messages1”没有在我的代码中被识别?我觉得这与代码的权限有关,但是当我从消息 1 TextView 中删除“最终”部分时,我得到了同样的错误。

困惑!

Here is the entire code to the class:

public class Homepage extends Activity {






    //URL to get JSON Arrays
   public static String url = "http://10.0.2.2/android/SQL.php?username='";
   public static String usernamefromlogin;
   public static TextView errorchecking; 

    //JSON Node Names 
    private static final String TAG_USER = "users";
    private static final String TAG_WINS = "wins";
    private static final String TAG_MESSAGES = "messages";
    private static final String TAG_NAME = "fullname";
    private static final String TAG_DISPLAY = "displayname";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_PW = "password";
    private static final String TAG_CREATED = "created_at";
    private static final String TAG_UPDATED = "updated_at";


    JSONArray user = null;

    //disable back button
    @Override
    public void onBackPressed() {

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reshomepage);


        //Get login name from EditText in login screen and concatenate it to PHP user-name for _GET command in 3 steps.
        //Step 1: Get intent from previous activity
        Intent intent = getIntent();
        getIntent().getExtras();




        //Step 2: convert intent (intent) to string called "usernamefromlogin"     //error checking in log cat to see value of "usernamefromlogin"      
       usernamefromlogin = intent.getExtras().getString("username2");      Log.d("log of usernamefromlogin", usernamefromlogin);

        //Step 3: take the string "url" and add string "usernamefromlogin" after it
       String url5 = url.concat(usernamefromlogin);
       String url6 = url5.concat("'");
        //find TextView "errorchecking" and send the string "url6" to it so it can display in log cat
       Log.d("log of URL6 in it's final state", url6);







        // Creating new JSON Parser
        JSONParser jParser = new JSONParser();

        // Getting JSON from URL from the final string "url6"
        JSONObject json = jParser.getJSONFromUrl(url6);
        //Logcat check value for TAG_USER



        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

            final TextView name1 = (TextView)findViewById(R.id.tvfullname);
             TextView messages1 = (TextView)findViewById(R.id.envelope);
            final TextView wins1 = (TextView)findViewById(R.id.wins);
            final TextView created1 = (TextView)findViewById(R.id.tvcreated_at);
            final 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();
    }

    }

    public void checkchallenge(View v) {
        String algo = null;
        if(algo == "123")
        {


              // display pop up message (toast)
            Context context = getApplicationContext();
            CharSequence text = "You have " + messages1 ;
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();


        }else
        {
        // display pop up message (toast)
            Context context = getApplicationContext();
            CharSequence text = "You have no new Challenges";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

        }
    }



}

【问题讨论】:

  • 变量“messages”在它上面的一些代码中被调用:有一个像variable scope这样的东西。您只能使用在您当前范围内可见的变量。
  • 对不起,我不太明白你的评论。那么如果变量用了一次,就不能再用了?我习惯于在 PHP 中编程,其中一个变量可以在同一个脚本上无限期地使用,所以这让我很困惑。
  • 可能有很多原因。 ..called in some code above it 不是很清楚。请显示您正在执行此操作的代码结构。我们或许可以提供更好的帮助。
  • 我刚刚更新了整个代码

标签: java android eclipse variables permissions


【解决方案1】:

我怀疑代码在方法中生成了String 消息和TextView 局部变量。如果您想在整个类中访问这些对象,则应将它们声明为字段。

public SomeClass{
    String messages;
    final TextView messages1;

    public void checkchallenge(View v) {
        //Method implementation
    }

    public void someOtherMethod(){
        this.messages = c.getString(TAG_MESSAGES);
        this.messages1 = (TextView)findViewById(R.id.envelope);
    }
}

【讨论】:

    【解决方案2】:

    如果您的消息变量是在其他方法中定义的,它将不会在您显示的位置显示。

    【讨论】:

    • 我刚刚用我的整个代码更新了我的问题。你认为这是因为我在 try/catch 语句中有字符串吗?
    • 可以肯定的是,所有变量都在 try/catch 范围内,在外部它们不存在。
    • 感谢您的帮助。我想我的新问题是,如何在不破坏代码的情况下删除 try and catch?我尝试删除它们,然后一切都变得混乱,我现在不断收到所有字符串的“未处理的异常类型 JSONException”
    • @user 我认为这种学习 java 的策略不会奏效。不要每次遇到问题就停下来寻求解决方案。浏览一些教程并使用谷歌。尝试理解错误,Java 的文档非常完善。
    • @SotiriosDelimanolis 你是对的 :) 我在谈论(写作?)关于停止和寻求帮助的策略。干杯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多