我的第一个Android应用程序出现问题。 我无法弄清楚为什么,等待5秒后,应用程序退出并说“不幸的是APP停止了”。该应用程序非常基本。只需添加和分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 package 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();
}
}
logcat的
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
问题是从泼溅到实际的应用程序。 separeted他们都很好。 感谢
编辑:在清单发生了变化
action android:name=".STARTINGPOINT"
到
action android:name="com.example.helloworld.STARTINGPOINT"
因为它是这样的。我改变了尝试让思考工作。遗憾
答案 0 :(得分:1)
从您发布的代码看,似乎没有“STARTINGPOINT”,只有Splash和MainActivity,需要在清单中声明。
需要将此添加到您的清单中:
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
而且,在Splash中,您需要通过intent启动MainActivity,
Intent openStartingPoint = new Intent(Splash.this, MainActivity.class);
其余部分看起来不错,你有没有名为STARTINGPOINT的活动?或者只是从清单中删除它:
<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”,这是您的例外。
答案 1 :(得分:1)
您正在尝试启动名为STARTINGPOINT的活动,但您没有该名称的类。
您的Android清单活动android:name属性应该与活动java文件的包/类名相同。将其更改为
android:name="com.example.helloworld.MainActivity"
此外,您可以在不使用字符串文字的情况下使用您的意图:
Intent intent = new Intent(this, MainActivity.class);