无错误仍显示模拟器中的BLANK屏幕

时间:2017-10-26 17:12:49

标签: android-studio

我写了以下代码。我想将我的Android应用程序与我的数据库连接。并从中获取数据并在应用程序中显示它。

以下是我的代码::

public class MainActivity extends AppCompatActivity{

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

        init();
    }



    public Connection CreateDBconn()
    {
        try
        {
            String DRIVER_NAME = "com.mysql.jdbc.Driver";
            String HOST_NAME = "firstdb.cqmpw4u1tyz3.us-west-2.rds.amazonaws.com";
            String PORT_NUMBER = "3306";
            String DB_NAME = "test_master";
            String USER_NAME = "testuser";
            String PASSWORD = "***********";
            String URL="jdbc:mysql://firstdb.cqmpw4u1tyz3.us-west-2.rds.amazonaws.com/test_master";
            Class.forName(DRIVER_NAME);
            Connection conn =   DriverManager.getConnection(URL,USER_NAME,PASSWORD);
            return conn;

        }
        catch(Exception e)
        {
            e.printStackTrace();
            System.out.print("hjdjksdhfkhsdkf");
        }
        return null;
    }

    public void init() {
try
{
        Connection c=CreateDBconn();
        String query="SELECT * FROM STUD;";
        Statement st=c.createStatement();
        ResultSet rs=st.executeQuery(query);

        TableLayout stk = (TableLayout) findViewById(R.id.TL);
        TableRow tbrow0 = new TableRow(this);
        TextView tv0 = new TextView(this);
        tv0.setText(" Sl.No ");
        tv0.setTextColor(Color.WHITE);
        tbrow0.addView(tv0);
        TextView tv1 = new TextView(this);
        tv1.setText(" Last Name ");
        tv1.setTextColor(Color.WHITE);
        tbrow0.addView(tv1);
        TextView tv2 = new TextView(this);
        tv2.setText(" First Name ");
        tv2.setTextColor(Color.WHITE);
        tbrow0.addView(tv2);
        TextView tv3 = new TextView(this);
        tv3.setText(" Age ");
        tv3.setTextColor(Color.WHITE);
        tbrow0.addView(tv3);
        TextView tv4 = new TextView(this);
        tv4.setText(" Branch ");
        tv4.setTextColor(Color.WHITE);
        tbrow0.addView(tv4);
        stk.addView(tbrow0);

        while (rs.next()) {
            TableRow tbrow = new TableRow(this);
            TextView t1v = new TextView(this);
            t1v.setText(rs.getInt("id"));
            t1v.setTextColor(Color.WHITE);
            t1v.setGravity(Gravity.CENTER);
            tbrow.addView(t1v);
            TextView t2v = new TextView(this);
            t2v.setText(rs.getString("LastName"));
            t2v.setTextColor(Color.WHITE);
            t2v.setGravity(Gravity.CENTER);
            tbrow.addView(t2v);
            TextView t3v = new TextView(this);
            t3v.setText(rs.getString("FirstName"));
            t3v.setTextColor(Color.WHITE);
            t3v.setGravity(Gravity.CENTER);
            tbrow.addView(t3v);
            TextView t4v = new TextView(this);
            t4v.setText(rs.getInt("Age"));
            t4v.setTextColor(Color.WHITE);
            t4v.setGravity(Gravity.CENTER);
            tbrow.addView(t4v);
            TextView t5v=new TextView(this);
            t5v.setText(rs.getString("Branch"));
            t5v.setTextColor(Color.WHITE);
            t5v.setGravity(Gravity.CENTER);
            tbrow.addView(t5v);
            stk.addView(tbrow);
        }}
catch (Exception e)
{
    e.printStackTrace();
    System.out.print("124587");
}

    }}

代码显示无错误。但在模拟器中它显示空白屏幕。没有打印数据。我甚至给了TRY和CATCH声明。 Catch Printout也没有发生。

问题是什么!?

1 个答案:

答案 0 :(得分:0)

空白?我认为是黑屏。

对我来说,我的所有虚拟设备上都有黑屏,直到我更新了我的显卡R9 380x。

完美地工作。

希望这也适合你。