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