如果没有对主活动的静态引用,setContentView将无法在安卓上运行

我创建了一个登录活动,它提供了应用程序的登录界面。

public class LoginActivity extends Activity {

我为它扩展了Activity类,然后覆盖了onCreate方法,如下所示:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);

        this.username = (TextView) findViewById(R.id.loginUsername);
        this.password = (TextView) findViewById(R.id.passwordLogin);

        this.username.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

        ImageView banner = (ImageView) findViewById(R.id.imageView5);
        banner.setImageResource(R.drawable.banner);
    } 

问题是.。模拟器上什么也没显示出来。

但是,这可以很好地工作:

MainActivity.getInstance().setContentView(R.layout.activity_login);

但这是一种非常实用的做法,而且会导致频繁的崩溃。会出什么问题呢?未显示控制台错误。

LoginActivity类:

package flarehubpe.xflare.flarehub;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.InputType;
import android.widget.ImageView;
import android.widget.TextView;
import android.os.Bundle;

import flarehubpe.xflare.flarehub.utils.jsonData;

import org.json.JSONException;

public class LoginActivity extends Activity {

    public TextView username;
    public TextView password;

    public boolean authenticated = false;
    public ProfileActivity profileManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);

        this.username = (TextView) findViewById(R.id.loginUsername);
        this.password = (TextView) findViewById(R.id.passwordLogin);

        this.username.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

        ImageView banner = (ImageView) findViewById(R.id.imageView5);
        banner.setImageResource(R.drawable.banner);
    }

    public void spawnToDevice(){
    }

    public void login(){
        try {
            jsonData data = RestAPI.getResponse("http://xxx/login.php?username=" + this.username.getText().toString() + "&password=" + this.password.getText().toString());
            if(data == null){
                sendAlert("Error", "Something went wrong... This is embarrassing. Please contact @ on twitter.","Ok");
                return;
            }
            if(!data.getString("error").contains("false")){
                sendAlert("Uh oh", data.getString("error"), "Ok");
                return;
            }
            sendAlert("Success!", "You are now logged in.", "Ok");
            createProfile(this.username.getText().toString(), this.password.getText().toString(), data.getInteger("coins"), data.getInteger("rankid"), data.getInteger("wins"), data.getInteger("kills"), data.getInteger("deaths"));
            this.username.setText("");
            this.password.setText("");
            this.authenticated = true;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void createProfile(String username, String password, Integer coins, Integer randid, Integer wins, Integer kills, Integer deaths){
        this.profileManager = new ProfileActivity(username, password, coins, randid, wins, kills, deaths);
    }

    public void sendAlert(String title, String content, String buttonText) {
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle(title);
        alertDialog.setMessage(content);
        alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, buttonText,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        alertDialog.show();
    }

    public void logout(){
        this.authenticated = false;
        this.spawnToDevice();
    }
}

转载请注明出处:http://www.yihangfood.com/article/20230526/2180907.html