2014-03-19 295 views
0

我從新波士頓教程創建了我的應用程序,但問題是啓動畫面無法正常工作。我的應用程序跳過啓動畫面,然後啓動MainActivity。我檢查了StackOverflow上的其他問題和答案,但他們沒有奏效。我在這裏粘貼我的代碼。請檢查並告訴我最新的問題。我的Android應用程序不加載應用程序屏幕,只是跳到主應用程序屏幕?

這裏是我的啓動畫面佈局文件: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> 

現在這裏是閃屏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; 
} 

} 

在這裏到底是清單文件:

<?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字節的內存不足ocation。 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:二進制XML文件行#2:錯誤膨脹類android.widget.RelativeLayout 03-19 19:43:07.049:E/AndroidRuntime( 24779):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308) 03-19 19:43:07.049:E/AndroidRuntime(24779):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362) 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):at android.os.Handler.dispatchMessage(Handler.java:99) 03-19 19:43:07.049:E/AndroidRuntime(24779):at android.os.Looper.loop(Looper.java:176) 03-19 19:43:07.049:E/AndroidRuntime(24779):at android.app.ActivityThread.main(ActivityThread.java:5493) 03-19 19/43:07.049:E/AndroidRuntime(24779):在java.lang.reflect.Method.invokeNative(本地方法) 03-19 19:43:07.049:E/AndroidRuntime(24779):在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):在dalvik.system.NativeStart.main(本地方法) 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):at 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):at android .view.LayoutInflater.onCreateView(LayoutInflater.java:718) 03-19 19:43:07。049:E/AndroidRuntime(24779):at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743) 03-19 19:43:07.049:E/AndroidRuntime(24779):at 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):at android.app.Activity.performCreate(Activity.java:5372) 03-19 19:43:07.049:E/AndroidRuntime(24779):at 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:電子/ 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):在java.lang.reflect.Constructor.constructNative(本地方法) 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):at android.view.L ayoutInflater.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(本地方法) 03-19 19:43:07.049:E/AndroidRuntime 24779):at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:586) 03-19 19:43:07.049:E/AndroidRuntime(24779):at 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:2 988) 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( (View.java:3564) 03-19 19:43:07.049:E/AndroidRuntime(24779):at android.view.View。(View.java:3493) 03 -19 19:43:07.049:E/AndroidRuntime(24779):at android.view.ViewGroup。(ViewGroup.java:469) 03-19 19:43:07.049:E/AndroidRuntime(24779):at android.widget .RelativeLayout。(RelativeLayout.java:242) 03-19 19:43:07.049:E/AndroidRuntime(24779):...26多

+0

這不是創建一個啓動畫面使用本教程創建一個啓動畫面HTTP的正確方法://www.androidhive .info/2013/07/how-to-implement-android-splash-screen-2/ –

+0

@NARESHREDDY我認爲我做的方式也是對的?因爲它的功能相同:( –

回答

0

需要改變

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

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

更新:在您的飛濺XML

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

有問題,你在哪裏做了修改(MyPage.this,MainActivity.this),有一條RED LINE,在它下面,快速修復無法解決問題。 –

+0

現在你可以檢查並享受 – Piyush

+0

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

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); 
+0

使用你的班級名稱,因爲我在代碼中使用了我自己的活動名稱。 – user8938

+0

這不是有趣! –

+0

設置斷點並檢查它停止的位置 – user8938