eclipse 开始一个新的活动 (android)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4511426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Starting a new activity (android)
提问by Mooh
i'm tring to start a new activity from the main one but i'm getting an error when the application is launched the application ... has stopped unexpectedly. please try again(no error on eclipse ). that's my code:
我正在尝试从主要活动开始一项新活动,但在启动应用程序时出现错误,应用程序...意外停止。请再试一次(eclipse 上没有错误)。那是我的代码:
public class clubber extends Activity {
protected SearchData sData ;
protected boolean club;
protected boolean resto;
protected boolean bar;
protected String searchTerms;
protected String city;
protected String distance;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
(...)
//Search button action
final Button button = (Button) findViewById(R.id.search);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
Toast.makeText(clubber.this, "Let's find something !", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setClass(clubber.this, Map.class);
startActivity(intent);
}
});
}
(...)
}
the target class:
目标类:
package com.clubber;
import android.os.Bundle;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
public class Map extends MapActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
MapView mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
androidmanifest.xml:
androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.club"
android:versionCode="1"
android:versionName="1.0">
<uses-library android:name="com.google.android.maps" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="com.clubber.ClubberActivity" 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=".Map"></activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
map.xml:
地图.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="*******************************"
/>
<LinearLayout android:id="@+id/zoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
logcat :
日志猫:
D/AndroidRuntime( 512): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
D/AndroidRuntime( 512): CheckJNI is ON
D/AndroidRuntime( 512): Calling main entry com.android.commands.am.Am
I/ActivityManager( 60): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.club/com.clubber.ClubberActivity } from pid 512
I/ActivityManager( 60): Start proc com.club for activity com.club/com.clubber.ClubberActivity: pid=520 uid=10036 gids={3003, 1015}
D/AndroidRuntime( 512): Shutting down VM
I/AndroidRuntime( 512): NOTE: attach of thread 'Binder Thread #3' failed
D/dalvikvm( 512): GC_CONCURRENT freed 102K, 69% free 319K/1024K, external 0K/0K, paused 1ms+3ms
D/dalvikvm( 512): Debugger has detached; object registry had 1 entries
W/dalvikvm( 520): Unable to resolve superclass of Lcom/clubber/Map; (36)
W/dalvikvm( 520): Link of class 'Lcom/clubber/Map;' failed
E/dalvikvm( 520): Could not find class 'com.clubber.Map', referenced from method com.clubber.ClubberActivity.onClick
W/dalvikvm( 520): VFY: unable to resolve const-class 35 (Lcom/clubber/Map;) in Lcom/clubber/ClubberActivity;
D/dalvikvm( 520): VFY: replacing opcode 0x1c at 0x0013
D/dalvikvm( 520): VFY: dead code 0x0015-0022 in Lcom/clubber/ClubberActivity;.onClick (Landroid/view/View;)V
I/ActivityManager( 60): Displayed com.club/com.clubber.ClubberActivity: +2s131ms
D/dalvikvm( 129): GC_EXPLICIT freed 70K, 50% free 2952K/5831K, external 2638K/3237K, paused 103ms
D/AndroidRuntime( 520): Shutting down VM
W/dalvikvm( 520): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime( 520): FATAL EXCEPTION: main
E/AndroidRuntime( 520): java.lang.NoClassDefFoundError: com.clubber.Map
E/AndroidRuntime( 520): at com.clubber.ClubberActivity.onClick(ClubberActivity.java:121)
E/AndroidRuntime( 520): at android.view.View.performClick(View.java:2485)
E/AndroidRuntime( 520): at android.view.View$PerformClick.run(View.java:9080)
E/AndroidRuntime( 520): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 520): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 520): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 520): at android.app.ActivityThread.main(ActivityThread.java:3647)
E/AndroidRuntime( 520): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 520): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 520): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 520): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 520): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 60): Force finishing activity com.club/com.clubber.ClubberActivity
W/ActivityManager( 60): Activity pause timeout for HistoryRecord{406b0d20 com.club/com.clubber.ClubberActivity}
D/dalvikvm( 60): GC_CONCURRENT freed 1010K, 47% free 4446K/8263K, external 2046K/2554K, paused 11ms+10ms
回答by danizmax
There are two things wrong with the manifest:
清单有两个问题:
<activity android:name=".Map"></activity>
should be outside the other activityand in application.
应该在其他活动之外并在应用程序中。
<uses-library android:name="com.google.android.maps" />
should be inside application
应该在应用程序内部
Use this xml:
使用这个xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.clubber"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".clubbera" 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=".Map"> </activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
回答by Mark B
You are missing the Map activity in your manifest. The stacktrace in logcat is probably telling you that is the error...
您的清单中缺少 Map 活动。logcat 中的堆栈跟踪可能会告诉您这是错误...
回答by Sankar Ganesh
pls ensure your Logcat, that you had yield, Activity Not Found Exception, this is because you had forgot to register your map activity in Android manifest file.
请确保您的 Logcat,您有 yield,Activity Not Found Exception,这是因为您忘记在 Android 清单文件中注册您的地图活动。
you have to add the Activity as shown below
您必须添加活动,如下所示
<activity android:name=".clubber" 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=".Map"></activity>
</application>
回答by gicuta13
In order to have this running you need to put in the manifest file the following line:
为了运行它,您需要在清单文件中添加以下行:
<uses-library android:name="com.google.android.maps" />
but please pay attention and add the line inside the <application>
tag like bellow:
但请注意并在<application>
标签内添加如下行:
<application>
...
<uses-library android:name="com.google.android.maps" />
</application>