java - 从启动画面到实际的应用程序。不幸的是APP已停止

标签 java android

我的第一个 Android 应用程序遇到问题。 我不明白为什么,等待 5 秒后,应用程序退出并显示“不幸的是应用程序已停止”。该应用程序非常基本。只需添加并减去 1 个数字即可。但启动时它显示图像。

MainActivity.java

package com.example.helloworld;

 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 MainActivity extends Activity {

        int counter;
        Button add, sub;
         TextView display;


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

    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.main, menu);
    return true;
}

 }

HelloWorldManifest.java

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

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

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

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


</application>

还有 Splash.java 包 com.example.helloworld;

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

 public class Splash 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 {
                Thread.sleep(5000);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally{

                Intent openStartingPoint = new Intent("com.example.helloworld.STARTINGPOINT");
                startActivity(openStartingPoint);
            }
        }

    };
    timer.start();
}

  }

日志记录

 04-25 08:25:33.391: D/AndroidRuntime(1016): Shutting down VM
 04-25 08:25:33.391: W/dalvikvm(1016): threadid=1: thread exiting with uncaught exception (group=0x41465700)
 04-25 08:25:33.471: E/AndroidRuntime(1016): FATAL EXCEPTION: main
 04-25 08:25:33.471: E/AndroidRuntime(1016): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.helloworld/com.example.helloworld.startingPoint}: java.lang.ClassNotFoundException: Didn't find class "com.example.helloworld.startingPoint" on path: DexPathList[[zip file "/data/app/com.example.helloworld-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.helloworld-1, /system/lib]]
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at android.os.Handler.dispatchMessage(Handler.java:99)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at android.os.Looper.loop(Looper.java:137)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at android.app.ActivityThread.main(ActivityThread.java:5103)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at java.lang.reflect.Method.invokeNative(Native Method)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at java.lang.reflect.Method.invoke(Method.java:525)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at dalvik.system.NativeStart.main(Native Method)
 04-25 08:25:33.471: E/AndroidRuntime(1016): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.helloworld.startingPoint" on path: DexPathList[[zip file "/data/app/com.example.helloworld-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.helloworld-1, /system/lib]]
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
 04-25 08:25:33.471: E/AndroidRuntime(1016):    ... 11 more
 04-25 08:35:02.411: D/dalvikvm(1066): GC_FOR_ALLOC freed 52K, 7% free 2631K/2800K, paused 42ms, total 44ms

问题是从启动到实际的应用程序。分开后他们都工作得很好。 谢谢

编辑: list 已更改

action android:name=".STARTINGPOINT"

action android:name="com.example.helloworld.STARTINGPOINT" 

因为事情就是这样。我改变了尝试让想法发挥作用。抱歉

最佳答案

从您发布的代码来看,似乎没有“STARTINGPOINT”,只有 Splash 和 MainActivity,需要在 list 中声明。

需要将其添加到您的 list 中:

<activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
   </activity>  

并且,在 Splash 中,您需要通过 Intent 启动 MainActivity,

Intent openStartingPoint = new Intent(Splash.this, MainActivity.class);  

其余部分看起来不错,您有任何名为 STARTINGPOINT 的 Activity 吗?或者只是从 list 中删除它:

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

编译器无法识别类“STARTINGPOINT”,这是您的异常(exception)情况。

关于java - 从启动画面到实际的应用程序。不幸的是APP已停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23294661/

相关文章:

java - 带参数的泛型方法与带通配符的非泛型方法

java - java中的表达式语言

Android:铃声在设置>声音和显示>手机铃声中不可见

android - 当前目录上的符号链接(symbolic link)级别太多?

安卓doInBackground

java - 如何在窗口关闭后正确停止 Java 小程序

java - 即使 `failsOnError` 设置为 `true`,Maven Checkstyle 插件在构建期间也不会失败

java - getFailedLoginAttempts() 函数在 Vaadin 的 appfoundation 中没有任何意义

java - 滑动抽屉未关闭

android - 如何从 Android 应用程序启动 ESC/POS 的切纸器