我的Android应用程序没有加载应用程序屏幕它只是跳到主应用程序屏幕?

时间:2014-03-19 13:33:28

标签: java android

我是从新的波士顿教程创建我的应用程序,但问题是启动画面不起作用。我的应用程序跳过启动画面,然后启动MainActivity。我在StackOverflow上检查了其他问题和答案但是它们没有用。我在这里粘贴我的代码。请检查并告诉我这是什么问题。

这是我的Splash屏幕布局文件:splash.xml

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

</RelativeLayout>

现在这里是Splash屏幕.java文件:MyPage.java

package com.example.yoyo;

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

public class MyPage extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    Thread timer = new Thread() {
        public void run() {
            try {
            sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            finally {
                Intent newActivity = new Intent("android.intent.action.MAIN");
                startActivity(newActivity);
            }

            }

            }; timer.start();
        };

}

现在是第二个屏幕,这是我的应用程序屏幕。它是布局文件:activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
    android:id="@+id/nameMe"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:ems="10"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/changeYou"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/nameMe"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"
    android:text="Tell Me Your Name" />

<TextView
    android:id="@+id/nameDisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />

</RelativeLayout>

这是第二个屏幕.java文件:MainActivity.java

package com.example.yoyo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

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

    final EditText nameMe = (EditText) findViewById(R.id.nameMe);
    Button changeYou = (Button) findViewById(R.id.changeYou);
    final TextView nameDisplay = (TextView) findViewById(R.id.nameDisplay);

    changeYou.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
        String  myName = nameMe.getText().toString();
        if (myName.contentEquals("pranav")) {
            nameDisplay.setText("Hey " + myName + ". Thanks for giving  Me Life. I love talking.");
        }else if (myName.contentEquals("harshit")) {
            nameDisplay.setText("Oye Chotte");
        }else if (myName.contentEquals("preety")) {
            nameDisplay.setText("Hello Mamma.");
        }else if (myName.contentEquals("ashok")) {
            nameDisplay.setText("Hello Papa");

        }else if (myName.contentEquals("akshit")) {
            nameDisplay.setText("Hello. Thanks for Using the app.");
        }else if(myName.contentEquals("clear")) {
            nameDisplay.setText("");
        }
        }
    });
}

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

}

最后这里是Manifest文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yoyo"
    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" android:debuggable="true">

    <activity android:name="com.example.yoyo.MyPage" 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.example.yoyo.MainActivity"></activity>
    </application>
</manifest>

这是日志文件:

03-19 19:43:07.039:E / dalvikvm-heap(24779):56819728字节分配的内存不足。 03-19 19:43:07.049:E / AndroidRuntime(24779):致命异常:主要 03-19 19:43:07.049:E / AndroidRuntime(24779):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.yoyo / com.example.yoyo.MyPage}:android.view.InflateException:Binary XML文件行#2:错误膨胀类android.widget.RelativeLayout 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.app.ActivityThread.access $ 700(ActivityThread.java:168) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1329) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.os.Handler.dispatchMessage(Handler.java:99) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.os.Looper.loop(Looper.java:176) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.app.ActivityThread.main(ActivityThread.java:5493) 03-19 19:43:07.049:E / AndroidRuntime(24779):at java.lang.reflect.Method.invokeNative(Native Method) 03-19 19:43:07.049:E / AndroidRuntime(24779):at java.lang.reflect.Method.invoke(Method.java:525) 03-19 19:43:07.049:E / AndroidRuntime(24779):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1225) 03-19 19:43:07.049:E / AndroidRuntime(24779):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041) 03-19 19:43:07.049:E / AndroidRuntime(24779):at dalvik.system.NativeStart.main(Native Method) 03-19 19:43:07.049:E / AndroidRuntime(24779):引起:android.view.InflateException:二进制XML文件行#2:错误膨胀类android.widget.RelativeLayout 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.view.LayoutInflater.createView(LayoutInflater.java:669) 03-19 19:43:07.049:E / AndroidRuntime(24779):at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.view.LayoutInflater.onCreateView(LayoutInflater.java:718) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.view.LayoutInflater.inflate(LayoutInflater.java:498) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.view.LayoutInflater.inflate(LayoutInflater.java:426) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.view.LayoutInflater.inflate(LayoutInflater.java:382) 03-19 19:43:07.049:E / AndroidRuntime(24779):at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:361) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.app.Activity.setContentView(Activity.java:1956) 03-19 19:43:07.049:E / AndroidRuntime(24779):at com.example.yoyo.MyPage.onCreate(MyPage.java:14) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.app.Activity.performCreate(Activity.java:5372) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270) 03-19 19:43:07.049:E / AndroidRuntime(24779):... 11更多 03-19 19:43:07.049:E / AndroidRuntime(24779):引起:java.lang.reflect.InvocationTargetException 03-19 19:43:07.049:E / AndroidRuntime(24779):at java.lang.reflect.Constructor.constructNative(Native Method) 03-19 19:43:07.049:E / AndroidRuntime(24779):at java.lang.reflect.Constructor.newInstance(Constructor.java:417) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.view.LayoutInflater.createView(LayoutInflater.java:643) 03-19 19:43:07.049:E / AndroidRuntime(24779):......还有23个 03-19 19:43:07.049:E / AndroidRuntime(24779):引起:java.lang.OutOfMemoryError 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:586) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:832) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.content.res.Resources.loadDrawable(Resources.java:2988) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.content.res.TypedArray.getDrawable(TypedArray.java:602) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.view.View。(View.java:3564) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.view.View。(View.java:3493) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.view.ViewGroup。(ViewGroup.java:469) 03-19 19:43:07.049:E / AndroidRuntime(24779):在android.widget.RelativeLayout。(RelativeLayout.java:242) 03-19 19:43:07.049:E / AndroidRuntime(24779):... 26更多

2 个答案:

答案 0 :(得分:0)

需要更改

 Intent newActivity = new Intent("android.intent.action.MAIN");
 startActivity(newActivity);

 Intent newActivity = new Intent(MyPage.this , MainActivity.class);
 startActivity(newActivity);

更新:

 Handler handler = new Handler();
 handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            Intent newActivity = new Intent(MyPage.this , MainActivity.class);
            startActivity(newActivity);
        }
    }, 3000);


public class MyPage extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
 super.onCreate(savedInstanceState);
 setContentView(R.layout.splash);
 Handler handler = new Handler();
 handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub

            Intent newActivity = new Intent(MyPage.this , MainActivity.class);
            startActivity(newActivity);
        }
    }, 3000);

    }

更改启动xml
  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:background="@drawable/yoyo">"

  </RelativeLayout>

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

  </RelativeLayout>

答案 1 :(得分:0)

使用以下代码

Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
    @Override
    public void run() 
    {
     finish();
     Intent myIntent = new Intent(MainPage.this,DashBordActivity.class);
    startActivity(myIntent);
    }
    }, 1000);