1

我仍在学习 android 应用程序开发,当我开始使用 google maps api v2(尚未使用它)制作一个非常简单的应用程序后,当我在我的 Sony Xperia SL 上运行它时,它显示“不幸的是 myapp 停止了”。LogCat 中的红色消息是“无法打开文件进行读取”(两次)。

主菜单.java

public class MainMenu extends Activity {

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

    Button bntStart = (Button) findViewById(R.id.buttonStart);
}

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

MainActivity.java

public class MainActivity extends Activity {

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

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

}

活动主菜单.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:background="@drawable/menu_bg"
android:gravity="center_vertical"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" >

    <Button
        android:id="@+id/buttonStart"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:text="Start" />

    <Button
        android:id="@+id/buttonStop"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:text="Stop" />

</LinearLayout>

<Button
    android:id="@+id/buttonExit"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="5dp"
    android:text="Exit" />

</LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />

</RelativeLayout>

显现

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

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="16" />

<permission
    android:name="taxi.route.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="taxi.route.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

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

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

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyCwPOMQ3LaE81JN3wdY6DvUi7EIJdn0q-I" />

    <activity
        android:name="taxi.route.MainActivity"
        android:label="activity_main" >
    </activity>
</application>

</manifest>

堆栈跟踪(我不确定这是否是问题)

01-20 17:48:32.129: D/dalvikvm(663): Late-enabling CheckJNI
01-20 17:48:32.399: D/TextLayoutCache(663): Using debug level: 0 - Debug Enabled: 0
01-20 17:48:32.459: D/libEGL(663): loaded /system/lib/egl/libGLES_android.so
01-20 17:48:32.469: D/libEGL(663): loaded /system/lib/egl/libEGL_adreno200.so
01-20 17:48:32.469: D/libEGL(663): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
01-20 17:48:32.469: D/libEGL(663): loaded /system/lib/egl/libGLESv2_adreno200.so
01-20 17:48:32.509: I/Adreno200-EGLSUB(663): <ConfigWindowMatch:2078>: Format RGBA_8888.
01-20 17:48:32.519: D/memalloc(663): /dev/pmem: Mapped buffer base:0x5c67c000 size:27111424 offset:23343104 fd:64
01-20 17:48:32.519: E/(663): Can't open file for reading
01-20 17:48:32.519: E/(663): Can't open file for reading
01-20 17:48:32.529: D/OpenGLRenderer(663): Enabling debug mode 0
01-20 17:48:32.529: W/ResourceType(663): No package identifier when getting value for resource number 0x00000001
01-20 17:48:32.529: D/AndroidRuntime(663): Shutting down VM
01-20 17:48:32.529: W/dalvikvm(663): threadid=1: thread exiting with uncaught exception (group=0x40aa8210)
01-20 17:48:32.529: E/AndroidRuntime(663): FATAL EXCEPTION: main
01-20 17:48:32.529: E/AndroidRuntime(663): android.content.res.Resources$NotFoundException: Resource ID #0x1
01-20 17:48:32.529: E/AndroidRuntime(663):  at android.content.res.Resources.getValue(Resources.java:1023)
01-20 17:48:32.529: E/AndroidRuntime(663):  at android.content.res.Resources.loadXmlResourceParser(Resources.java:2110)
01-20 17:48:32.529: E/AndroidRuntime(663):  at android.content.res.Resources.getLayout(Resources.java:862)
01-20 17:48:32.529: E/AndroidRuntime(663):  at android.view.MenuInflater.inflate(MenuInflater.java:92)
01-20 17:48:32.529: E/AndroidRuntime(663):  at taxi.route.MainMenu.onCreateOptionsMenu(MainMenu.java:21)
01-20 17:48:32.529: E/AndroidRuntime(663):  at android.app.Activity.onCreatePanelMenu(Activity.java:2444)
01-20 17:48:32.529: E/AndroidRuntime(663):  at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:392)
01-20 17:48:32.529: E/AndroidRuntime(663):  at com.android.internal.policy.impl.PhoneWindow.invalidatePanelMenu(PhoneWindow.java:743)
01-20 17:48:32.529: E/AndroidRuntime(663):  at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:2838)
01-20 17:48:32.529: E/AndroidRuntime(663):  at android.os.Handler.handleCallback(Handler.java:605)
01-20 17:48:32.529: E/AndroidRuntime(663):  at android.os.Handler.dispatchMessage(Handler.java:92)
01-20 17:48:32.529: E/AndroidRuntime(663):  at android.os.Looper.loop(Looper.java:137)
01-20 17:48:32.529: E/AndroidRuntime(663):  at android.app.ActivityThread.main(ActivityThread.java:4441)
01-20 17:48:32.529: E/AndroidRuntime(663):  at java.lang.reflect.Method.invokeNative(Native Method)
01-20 17:48:32.529: E/AndroidRuntime(663):  at java.lang.reflect.Method.invoke(Method.java:511)
01-20 17:48:32.529: E/AndroidRuntime(663):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
01-20 17:48:32.529: E/AndroidRuntime(663):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
01-20 17:48:32.529: E/AndroidRuntime(663):  at dalvik.system.NativeStart.main(Native Method)
01-20 17:48:35.009: I/Process(663): Sending signal. PID: 663 SIG: 9
4

2 回答 2

0

由于以下行,您很可能会遇到问题MainMenu

getMenuInflater().inflate(1, menu);

inflate()需要一个指向 XML 菜单文件的资源 ID 和一个Menu对象。但是,1几乎可以肯定不是一个有效的 ID,从而导致异常。尝试将 ID 传递给 Menu XML 文件。

于 2013-01-20T15:42:50.057 回答
-1

习惯使用异常处理,这样您的应用程序就不会崩溃。至少做一些类似的事情:

try {
    /// my code
} catch (Trowable t) {
    Log.e("MyTag", t.getMessage(), t);
}

大多数时候你想做更多的事情:

try {
            /// my code
} catch (MostLikelyException mle) {
   /// Handle the exception
} catch (LikelyException le) {
   /// Handle the exception
} catch (Trowable t) {
   /// Handle the exception
}

由于您无法访问日志,因此一种解决方法是使用 toast 来获取异常信息。

将每个方法的代码包装在 try/catch 中。当你捕捉到异常时,用 Toast.makeText(this / pointer to the activity /, t.getMessage, Toast.LENGTH_LONG).show();处理它

找出您无法访问日志的原因应该是当务之急。

于 2013-01-20T16:36:40.560 回答