public 静态变量具有值,但是当我尝试将它显示给文本视图时(在另一个 class 中)它没有值

public static variable has the value , but when i try to show it to textview(in another class) it has no value

我的 activity 流程,登录页面->主页(在此我有一个按钮可以转到具有 onclick 方法的用户配置文件)-> 按钮 onclick 方法是 "startactivity(Userprofile)" 我有一个 onClick 方法加载 activity Userprofile。 我想加载用户的详细信息,我有一些 static 变量但运气不好。

public class Userprofile extends AppCompatActivity {
    TextView textView , textView1 , textView2 ;
    Context context_userprofile;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userprofile);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        func_userprofile_show();
    }

    public void func_userprofile_show(){
        String type = "linkuser";
        DatabaseLink databaseLink = new DatabaseLink(this);
        databaseLink.execute(type);
        context_userprofile = getApplicationContext();
        textView = (TextView)findViewById(R.id.tvprofilepage_user);
        assert textView != null;
        textView.setText(DatabaseLink.thename);
        textView1 = (TextView)findViewById(R.id.tvprofilepage_ign);
        assert textView1 != null;
        textView1.setText(DatabaseLink.theign);
        textView2 = (TextView)findViewById(R.id.tvprofilepage_emailid);
        assert textView2 != null;
        textView2.setText(DatabaseLink.theemail);
    }
}

现在这是来自不同 class 的方法,它扩展 AsyncTask 并且 "theign" 显示在 AlertDialog 中,但不显示在 TextView 中。

public void decodejson() {
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        details_json = jsonObj.getJSONArray(TAG_RESULTS);

        for (int i = 0; i < details_json.length(); i++) {
            JSONObject c = details_json.getJSONObject(i);
            thename = c.getString(TAG_NAME);
            theign= c.getString(TAG_IGN);
            theemail = c.getString(TAG_EMAIL);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

@Override
protected void onPreExecute() {
    alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("User Profile Loading...");
}

@Override
protected void onPostExecute(String result) {
    myJSON = result;
    decodejson();
    alertDialog.setMessage(theign);
    alertDialog.show();
}

问题是 textView.setText 在 AsyncTask 完成之前被调用。

要解决此问题,您可以实现一个接口,以便在信息准备好后调用 activity 中的函数。 假设 AsynkTask 在您的 DatabaseLink class 中,我将向您展示一些伪代码..

接口:

public interface DatabaseLinkInterface {
    public void databaseLinkFinished();
}

上下文也将是侦听器。您的 DatabaseLink class:

中有上下文
@Override
protected void onPostExecute(String result) {
    myJSON = result;
    decodejson();
    alertDialog.setMessage(theign);
    alertDialog.show();
    DatabaseLinkInterface mListener = (DatabaseLinkInterface) context;
    mListener.databaseLinkFinished();
}

在您的 activity 中实现接口并在 onCreate 中创建 DatabaseLink:

public class Userprofile extends AppCompatActivity implements DatabaseLinkInterface {
    TextView textView , textView1 , textView2 ;
    Context context_userprofile;
    DatabaseLink databaseLink;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userprofile);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        String type = "linkuser";
        databaseLink = new DatabaseLink(this);
        databaseLink.execute(type);
    }

    ...

最后,在您的 activity 中,实现接口方法以了解何时获取数据。删除 func_userprofile_show()。 避免为此目的使用静态变量。

@Override 
public void databaseLinkFinished(){
    context_userprofile = getApplicationContext();
    textView = (TextView)findViewById(R.id.tvprofilepage_user);
    assert textView != null;
    textView.setText(databaseLink.thename);
    textView1 = (TextView)findViewById(R.id.tvprofilepage_ign);
    assert textView1 != null;
    textView1.setText(databaseLink.theign);
    textView2 = (TextView)findViewById(R.id.tvprofilepage_emailid);
    assert textView2 != null;
    textView2.setText(databaseLink.theemail);
}

我还建议您使用 getter 和 setter 而不是直接访问属性 (databaseLink.theemail -> databaseLink.getTheEmail())