活动之间的转换(Android)

时间:2014-05-12 08:49:51

标签: java android

活动之间的转换无效。

应用程序的作用是打开android.xml文件,然后在5秒后打开主文件 这是代码:

android清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android"
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.example.android.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        </activity>
         <activity
        android:name="com.example.android.android"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.ANDROID" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        </activity>


</application>

android.java文件:

package com.example.android;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;

public class android extends Activity{
MediaPlayer sound1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.android);
    sound1 = MediaPlayer.create(android.this , R.raw.android);
    sound1.start();
    Thread timer = new Thread()
    {public void run()
        {
            try{
                sleep(5000);
            }
            catch(InterruptedException ac){
                ac.printStackTrace();
            }
            finally{
                Intent openactivity = new Intent            ("android.intent.action.MAIN");
            startActivity(openactivity);
            }

    }

};
timer.start();

 }

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    sound1.release();
    finish();
}
}

Mainactivity.java:

package com.example.android;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new               PlaceholderFragment()).commit();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

    }

android.xml文件:(启动的第一个文件)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background = "@drawable/android"    
>
</LinearLayout>

2 个答案:

答案 0 :(得分:1)

试试这种方式

 Intent openactivity = new Intent(android.this,MainActivity.class);
 startActivity(openactivity);

有关更多信息,请转到starting-new-activity

答案 1 :(得分:1)

首先,始终大写第一个类名(代码约定),

然后写下你的意图:

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

您可以在Android开发者网站上查看Starting Another Activity