无法重定向到下一页

时间:2014-04-21 10:23:13

标签: android android-manifest

这是我的Home.java代码。它不会重定向到下一页,即使我将意图更改为(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();}{
    }
}

这是我的MainActivity.java代码。我下一页 我收到错误为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;
        }
    }
}

清单如下:

<?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>

这是logcat错误:

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)

这是我的Register.java类

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;
    }

} }

5 个答案:

答案 0 :(得分:1)

<强>首先

改变您的意图:

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

<强>其次

只需用以下内容更新您的清单:

<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>

将此用作主页活动

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;
    }
}

答案 1 :(得分:0)

您可以使用此代码

替换

 .MainActivity

com.droid.mine.MainActivity

在Manifest文件中声明。

//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);

答案 2 :(得分:0)

我希望这有效:

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();
    }
}

编辑:用这个替换你的清单文件,现在我希望它有效:

<?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>

答案 3 :(得分:0)

您需要在清单中注册MainActivityRegister活动:

<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);
    });
}

答案 4 :(得分:0)

将您的意图更改为:

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

并在清单文件中使用:

    <?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>