2013-04-21 37 views
0

我剛剛嘗試在過去幾天學習Java。我一直在跳過教程等,試圖拿起一些基礎知識。需要幫助解決爲什麼我的簡單應用崩潰

目前它在啓動屏幕上啓動5秒鐘,然後轉到另一個Action(是正確的術語?),它是主頁(稱爲StartingPoint)。然而它在這兩頁之間跳躍時崩潰。所以基本上,我的問題是爲什麼它這樣做,我該如何解決它?

我首先創建了主頁面,並且可以自行工作。只是在這兩頁之間跳轉時纔會發生。

清單 - 修正:

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

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

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

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

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

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

Splash.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/mirley" 
> 


</LinearLayout> 

Splash.Java:我有一種感覺的編碼是錯誤的在這裏的某個地方。也許在Try Catch Finally?

package com.jonysapp.test; 

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

public class Splash extends Activity{ 

@Override 
protected void onCreate(Bundle MirleysVariable) { 
    // TODO Auto-generated method stub 
    super.onCreate(MirleysVariable); 
    setContentView(R.layout.splash); 
    Thread timer = new Thread(){ 
     public void run(){ 
      try{ 
       sleep(5000); 

      } catch (InterruptedException e){ 
       e.printStackTrace(); 
      }finally{ 
       Intent openStartingPoint = new Intent(Splash.this, StartingPoint.class); 
       Splash.this.startActivity(openStartingPoint); 
      } 
     } 
    }; 
    timer.start(); 

} 

} 

StartingPoint.Java

package com.jonysapp.test; 

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

public class StartingPoint extends Activity { 

int counter; 
Button add, sub; 
TextView display; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_starting_point); 
    counter = 0; 
    add = (Button) findViewById(R.id.bAdd); 
    sub = (Button) findViewById(R.id.bSub); 
    display = (TextView) findViewById(R.id.tvDisplay); 
    add.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      counter++; 
      display.setText("Your total is " + counter); 

     } 
    }); 
    sub.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      counter--; 
      display.setText("Your total is " + counter); 

     } 
    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.starting_point, menu); 
    return true; 
} 

} 

logcat的:

04-21 12:48:08.004: D/dalvikvm(31290): GC_FOR_ALLOC freed 66K, 3% free 8887K/9091K, paused 16ms 

04-21 12:48:08.014: I/dalvikvm-heap(31290): Grow heap (frag case) to 10.280MB for 1639696-byte allocation 

04-21 12:48:08.044: D/dalvikvm(31290): GC_CONCURRENT freed 1K, 3% free 10487K/10759K, paused 2ms+2ms 

04-21 12:48:08.084: D/TextLayoutCache(31290): Using debug level: 0 - Debug Enabled: 0 

04-21 12:48:08.134: D/libEGL(31290): loaded /system/lib/egl/libGLES_android.so 

04-21 12:48:08.134: D/libEGL(31290): loaded /system/lib/egl/libEGL_adreno200.so 

04-21 12:48:08.144: D/libEGL(31290): loaded /system/lib/egl/libGLESv1_CM_adreno200.so 

04-21 12:48:08.144: D/libEGL(31290): loaded /system/lib/egl/libGLESv2_adreno200.so 

04-21 12:48:08.174: D/OpenGLRenderer(31290): Enabling debug mode 0 

04-21 12:48:13.094: W/dalvikvm(31290): threadid=11: thread exiting with uncaught exception (group=0x2b542210) 

04-21 12:48:13.094: E/AndroidRuntime(31290): FATAL EXCEPTION: Thread-2196 

04-21 12:48:13.094: E/AndroidRuntime(31290): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.jonysapp.test/com.jonysapp.test.StartingPoint}; have you declared this activity in your AndroidManifest.xml? 

04-21 12:48:13.094: E/AndroidRuntime(31290): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508) 

04-21 12:48:13.094: E/AndroidRuntime(31290): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384) 

04-21 12:48:13.094: E/AndroidRuntime(31290): at android.app.Activity.startActivityForResult(Activity.java:3190) 

04-21 12:48:13.094: E/AndroidRuntime(31290): at android.app.Activity.startActivity(Activity.java:3297) 

04-21 12:48:13.094: E/AndroidRuntime(31290): at com.jonysapp.test.Splash$1.run(Splash.java:23) 

04-21 12:48:13.364: D/OpenGLRenderer(31290): Flushing caches (mode 0) 

04-21 12:48:13.404: D/OpenGLRenderer(31290): Flushing caches (mode 1) 

它很可能我已經發布了太多的代碼這個問題,所以我道歉,如果我有這個轟炸頁面太多。雖然如果我錯過了任何重要的幫助,請讓我知道!

作爲一個側面說明,如果您有任何提示,以阻止我進入任何不良的生活習慣與編碼到目前爲止,這將是又升值(但不是必要的)

感謝您的幫助!

+1

這是上帝的方式告訴你,閃屏是邪惡的;)http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/ – Simon 2013-04-21 12:30:34

+0

哈哈!那麼我在這一方面當然與神同在!出於興趣,有什麼替代(和更好)的方式來做這種事情而不是閃屏? – 2013-04-21 13:05:01

+0

什麼樣的事情?故意拖延開始和用戶獲取UI之間的時間?使用受限資源?我可以想到很多方法,但很少有人這麼做;) – Simon 2013-04-21 13:23:09

回答

0

見本的messge:

12月4日至21日:48:13.094:E/AndroidRuntime(31290): android.content.ActivityNotFoundException:無法找到明確 活動類{com.jonysapp.test /com.jonysapp.test.StartingPoint}; 你是否在你的AndroidManifest.xml中聲明瞭這個活動

這意味着你有一些你沒有在manifest文件中聲明的活動。您已使用所有資金在這裏:

com.jonysapp.test.STARTINGPOINT 

嘗試使:

com.jonysapp.test.StartingPoint. 

類的名稱是區分我猜敏感。 @Trinimon是對的。

+0

謝謝你的幫助,Shobhit!我試着在給Trinimon的評論中給你添加標籤,但如果不起作用,我想我會在這裏感謝你。但是,在更改清單(如大寫字母到小寫字母)之後,我仍然遇到錯誤。我用新的清單修改了我原來的帖子,你能否檢查修改後的版本並告訴我我是否正確地做了這些修改? – 2013-04-21 13:10:49

+0

嘗試在一個應用程序標籤中進行所有活動:例如:< activity>。現在,您的清單中有2個應用程序標籤。保持一個,並把兩個活動放在一個裏面。你也有同一個包的同一src文件夾中的類,對嗎? – 2013-04-21 13:15:56

+0

修好了!我仍然很難相信!感謝你和@Trinimon爲此。你們在解決這個問題上都有很大的幫助,而且我現在更瞭解logcat,因爲你指出了它提到的錯誤! – 2013-04-21 13:20:17

1

這是你的問題:

12月4日至21日:48:13.094:E/AndroidRuntime(31290): android.content.ActivityNotFoundException:無法找到明確的 活動類{com.jonysapp。 test/com.jonysapp.test.StartingPoint}; 你在你的AndroidManifest.xml中宣佈這一活動

你必須申報您的清單活動,即

<activity 
    android:name="com.example.project.StartingPoint" 
    android:label="@string/app_name"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 

附::爲應用啓動器活動添加了intent-filter

希望這有助於...乾杯!

+0

首先,感謝您和@Shobhit_Puri(我希望標記好的),以獲得如此驚人的快速響應! 試圖將com.jonysapp.test.STARTINGPOINT更改爲com.jonysapp.test.StartingPoint,它在5秒鐘睡眠後仍然崩潰。 我覺得我可能瞄準了錯誤的文件?我正確地試圖打開Java文件(StartingPoint.java),或者我應該試圖打開xml文件(在我的情況下它是activity_starting_point.xml)。我沒有把這個文件的代碼放在我原來的文章中,但是如果它可以幫助我可以添加它? 再次感謝您的幫助! – 2013-04-21 12:41:02

+0

親愛的喬納森,您需要將該活動添加到位於項目根目錄中的文件「AndroidManifest.xml」中。注意大寫/小寫字母:如果您的活動名爲'StartingPoint',則'com.example.project.STARTINGPOINT'將不起作用。 – Trinimon 2013-04-21 12:44:46

+0

親愛的Trinimon。我已經按照你所說的修改了艙單。但它仍然帶來了同樣的錯誤?我已將原始文章編輯到新的Manifest文件中,您能否確認它是如何看起來的?再次感謝你的時間,我相信你還有其他的事情,你寧願在週末做! – 2013-04-21 13:07:10