有没有办法解决局部变量可能尚未初始化

时间:2012-09-12 19:26:38

标签: java android

我收到的错误是局部变量可能未在以下代码行中初始化

final StingBuilder personSearchURLOne = personSearchURLOne.append(getName.getName1(searchOne));

我无法通过最终的StringBuilder来初始化personSearchURLOne = null;起初因为那时我将无法在以后更改它,所以我试图在我需要的实例中初始化它。它必须是最终的,因为如果它不是我得到错误:不能引用在不同方法中定义的内部类中的非final变量。

这是我的另一个班级

import android.widget.EditText;

public class getName {
static String nameOne = null;
static String nameTwo = null;

static StringBuilder personURLOne = new StringBuilder();
static StringBuilder personURLTwo = new StringBuilder();

public static String personURL = "http://api.themoviedb.org/3/search/person?api_key=bb0b6d66c2899aefb4d0863b0d37dc4e&query=";

public static StringBuilder getName1(EditText searchOne){
    nameOne = searchOne.getText().toString();


    nameOne = nameOne.replace(" ", "_");


    personURLOne.append(personURL); 
    personURLOne = personURLOne.append(nameOne);



    return personURLOne;

}

public static StringBuilder getName2(EditText searchTwo){
    nameTwo = searchTwo.getText().toString();
    nameTwo = nameTwo.replace(" ", "_");

    personURLTwo.append(personURL);
    personURLTwo = personURLTwo.append(nameTwo);

    return personURLTwo;
}


}

1 个答案:

答案 0 :(得分:2)

final StingBuilder personSearchURLOne = new StringBuilder(getName.getName1(searchOne));