Cannot redirect to next page

2019-07-30 14:55发布

问题:

Here is my Home.java code. It is not redirecting to next page, even if I change the intent to (home.this, MainActivity.class):

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class Home extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        Thread timer = new Thread() {
            public void run() {
                try{
                    sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    Intent openMainActivity = new   Intent("com.droid.mine.MainActivity");
                    startActivity(openMainActivity);
                }
            }
        };
        timer.start();}{
    }
}

Here is my MainActivity.java code. I.e next page I am getting the error as ClassCastException:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    Button log,sign;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        log = (Button) findViewById(R.id.register);
        sign = (Button) findViewById(R.id.linktologin);
        log.setOnClickListener((OnClickListener) this);
        sign.setOnClickListener((OnClickListener) this);
    }

    public void onClick(View v) {
        switch(v.getId()) {
        case R.id.register:
            break;
        case R.id.linktologin:
            Intent i = new Intent();
            i.setClass(MainActivity.this,Register.class);
            startActivity(i);
            break;
        }
    }
}

Manifest is as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.droid.mine"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.droid.mine.Home"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name=".MAINACTIVITY" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>   
    </application>
</manifest>

Here is the logcat error:

04-21 15:59:07.156: I/ApplicationPackageManager(21341): cscCountry is not German : INS
04-21 15:59:07.273: D/dalvikvm(21341): GC_EXTERNAL_ALLOC freed 78K, 47% free 2860K/5379K, external   408K/517K, paused 95ms
04-21 15:59:09.648: W/dalvikvm(21341): threadid=9: thread exiting with uncaught exception   (group=0x40018578)
04-21 15:59:09.710: E/AndroidRuntime(21341): FATAL EXCEPTION: Thread-10
04-21 15:59:09.710: E/AndroidRuntime(21341): android.content.ActivityNotFoundException: No Activity  found to handle Intent { act=com.droid.mine.MainActivity }
04-21 15:59:09.710: E/AndroidRuntime(21341):    at   android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1409)
04-21 15:59:09.710: E/AndroidRuntime(21341):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
04-21 15:59:09.710: E/AndroidRuntime(21341):    at android.app.Activity.startActivityForResult(Activity.java:2827)
04-21 15:59:09.710: E/AndroidRuntime(21341):    at android.app.Activity.startActivity(Activity.java:2933)
04-21 15:59:09.710: E/AndroidRuntime(21341):    at com.droid.mine.Home$1.run(Home.java:21)

Here is my Register.java class

package com.droid.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Register extends Activity implements View.OnClickListener {
Button backlog,regg;


protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    backlog = (Button) findViewById(R.id.loginn);
    regg = (Button) findViewById(R.id.signup);
    backlog.setOnClickListener(this);
}
public void onClick (View v)
{

    switch(v.getId()) {
    case R.id.loginn:
        break;
    case R.id.signup:
        Intent in = new Intent();
        in.setClass(Register.this,MainActivity.class);
        startActivity(in);
        break;
    }

} }

回答1:

Firstly

Change where you intent with this:

Intent intent = new Intent(Home.this,MainActivity.class);
startActivity(intent);    

Secondly

Just update your manifest with this:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.droid.mine.Home"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.droid.mine.MainActivity"
        android:label="@string/app_name" >

    </activity>
       <activity
        android:name="com.droid.mine.Register"
        android:label="@string/app_name" >

    </activity>     
</application>

Use this as home activity

package com.example.pms;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.widget.Toast;

public class SplashScreen extends Activity {        
    /*
     * The thread to process splash screen events
     */
    private Thread mSplashThread;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // splash screen view
        setContentView(R.layout.activity_splash);

        final SplashScreen mSplashScreen=this;
        /*
         * The thread to wait for splash screen events
         */
        mSplashThread =new Thread(){
            @Override
            public void run(){
                try {
                    synchronized(this){
                        // Wait given period of time or exit on touch
                        wait(3000);
                    }
                }
                catch(InterruptedException ex){                    
                }

                finish();

                // Run next activity
                startActivity(new Intent(getBaseContext(), MainActivity.class));
                new Runnable() {
                    @Override
                    public void run() {
                        mSplashThread.stop(); 
                    }
                };
            }
        };
        mSplashThread.start();   
    }
    /**
     * Processes splash screen touch events
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {    
        if(event.getAction() == MotionEvent.ACTION_DOWN)
        {
            synchronized(mSplashThread){
                mSplashThread.notifyAll();
            }
        }
        return true;
    }
}


回答2:

you can use this code instead

replace

 .MainActivity

with

com.droid.mine.MainActivity

while declaring in Manifest file.

//delay in ms
int DELAY = 1000;

Handler handler = new Handler();
handler.postDelayed(new Runnable() {            
    @Override
    public void run() {
        Intent intent = new Intent(Home.this,MainActivity.class);
        startActivity(intent);                  
    }
}, DELAY);


回答3:

Change your Intent to:

Intent openMainActivity = new Intent(Home.this, MainActivity.class);
                startActivity(openMainActivity);

and in manifest file use:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.testapp.Home"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >

        </activity>   
    </application>

</manifest>


回答4:

You need to register MainActivity and Register Activity in the manifest:

<activity android:name=".Mainactivity" />
<activity android:name=".Register" />

log.setOnClickListener(new View.OnClickListener()
{           
    @Override
    public void onClick(View v) 
    {
    });
}

sign.setOnClickListener(new View.OnClickListener()
{           
    @Override
    public void onClick(View v) 
    {
        Intent i = new Intent();
        i.setClass(MainActivity.this,Register.class);
        startActivity(i);
    });
}


回答5:

I hope this works:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class Home extends Activity{
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        Thread splashTimer = new Thread() {
            public void run() {
                try {
                    int splashTimer = 0;
                    while (splashTimer < 2000) {
                        sleep(100);
                        splashTimer = splashTimer + 100;
                    }

                    Intent intent;
                    intent = new Intent(Home.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    Log.d("asd","dasd");
                }
                finally {
                    finish();
                }
            }
        };
        splashTimer.start();
    }
}

Edit: Replace your manifest file with this, now I hope it works:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.droid.mine"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.droid.mine.Home"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.droid.mine.MainActivity"
            android:label="@string/app_name" >
        </activity>   
    </application>
</manifest>