有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java startActivity(i);显示错误

它在"startActivity(i);"中显示了一个错误

Error: startActivity (安卓.content.Intent) in Activity cannot be applied.

下面是代码:

public class LoadingScreen extends AppCompatActivity {

    private static int SplashInterval = 2000;

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

        new Handler().postDelayed(new Runnable(){
            @Override
            public void run(){
                //TODO Auto-generated method stub
                Intent i = new Intent(LoadingScreen.this, MainActivity.class);
                startActivity(i);

                this.finish();
            }
            private void finish(){
                //TODO Auto-generated method stub
            }
        },SplashInterval);
    };
}

共 (3) 个答案

  1. # 1 楼答案

    试试这个

     new Handler().postDelayed(new Runnable() {
    
            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */
    
            @Override
            public void run() {
                Intent i = new Intent(LoadingScreen.this, MainActivity.class);
                startActivity(i);
                finish();
    
                // close this activity
            }
        }, SplashInterval);
    
  2. # 2 楼答案

    拆下这条线

    this.finish();
    

    这是写在处理器内部的

    而是做LoadingScreen.this.finish(); 完成这项活动

  3. # 3 楼答案

    试试这个

    public class LoadingScreen extends Activity {
    
        private static int SplashInterval = 2000;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.loading_screen);
    
            new Handler().postDelayed(new Runnable(){
                @Override
                public void run(){
                    //TODO Auto-generated method stub
                    Intent i = new Intent(LoadingScreen.this, MainActivity.class);
                    startActivity(i);
                    finish();
                }
    
            },SplashInterval);
        };
    }