应用已崩溃(已停止)

时间:2014-10-21 09:28:41

标签: java android

我刚开始使用Android和Java开发,并在我的AVD模拟器上得到了这个。此外,我在这里发现了许多相同的问题,但说实话,我真的不知道如何修复它。我的应用程序只有3个按钮了。他们也不活跃..我只是把它们放在Graphical Layout。这是Logcat:

10-21 05:05:13.811: E/AndroidRuntime(2042): FATAL EXCEPTION: main
10-21 05:05:13.811: E/AndroidRuntime(2042): Process: com.example.reserved, PID: 2042
10-21 05:05:13.811: E/AndroidRuntime(2042): android.content.res.Resources$NotFoundException:Resource ID #0x7f070000
10-21 05:05:13.811: E/AndroidRuntime(2042):     at android.content.res.Resources.getValue(Resources.java:1123)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2309)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at android.content.res.Resources.getLayout(Resources.java:939)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at android.view.MenuInflater.inflate(MenuInflater.java:107)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at com.example.reserved.MainActivity.onCreateOptionsMenu(MainActivity.java:21)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at android.app.Activity.onCreatePanelMenu(Activity.java:2538)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at android.view.Choreographer.doFrame(Choreographer.java:543)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at android.os.Handler.handleCallback(Handler.java:733)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at android.os.Handler.dispatchMessage(Handler.java:95)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at android.os.Looper.loop(Looper.java:136)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at android.app.ActivityThread.main(ActivityThread.java:5017)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at java.lang.reflect.Method.invokeNative(Native Method)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at java.lang.reflect.Method.invoke(Method.java:515)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-21 05:05:13.811: E/AndroidRuntime(2042):     at dalvik.system.NativeStart.main(Native Method)

这是MainActivity中的第22行,其中是错误

package com.example.reserved;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

以下是Activity_main

中的按钮
<Button
    android:id="@+id/button1"
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="49dp"
    android:layout_marginTop="63dp"
    android:text="@string/Rest" />

<Button
    android:id="@+id/button2"
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/button1"
    android:layout_marginTop="15dp"
    android:text="@string/Rest1" />

<Button
    android:id="@+id/button3"
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/button2"
    android:layout_below="@+id/button2"
    android:layout_marginTop="15dp"
    android:text="@string/Rest2" />

这是main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.reserved.MainActivity" >

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

</menu>

已更新 - AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.reserved"
android:versionCode="1"
android:versionName="1.0" >

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        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>

</manifest>

1 个答案:

答案 0 :(得分:1)

你是否在strings.xml中添加了上面提到的所有字符串 请尝试使用MainActivity.java中的这么多代码来运行示例

package com.example.reserved;

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



public class MainActivity extends Activity {

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

}

你的activity_main.xml到

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<Button
    android:id="@+id/button1"
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="49dp"
    android:layout_marginTop="63dp"
    android:text="Rest" />

<Button
    android:id="@+id/button2"
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/button1"
    android:layout_below="@id/button1"
    android:layout_marginTop="15dp"
    android:text="Rest1" />

<Button
    android:id="@+id/button3"
    android:layout_width="220dp"
    android:layout_height="wrap_content"
    android:layout_alignRight="@id/button2"
    android:layout_below="@id/button2"
    android:layout_marginTop="15dp"
    android:text="Rest2" />
</RelativeLayout>