0

我正在使用 Hello Android 3rd edition 作为指南创建我的第一个应用程序。第二章是关于制作数独应用程序的教程。当我运行书中相同的代码时,它会给出错误“应用程序数独已意外停止”。请帮助。

日志猫

05-07 08:47:45.512: W/ResourceType(1310): Failure getting entry for 0x7f030002 (t=2 e=2) in package 0 (error -75)
05-07 08:47:45.512: D/AndroidRuntime(1310): Shutting down VM
05-07 08:47:45.512: W/dalvikvm(1310): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-07 08:47:45.572: E/AndroidRuntime(1310): FATAL EXCEPTION: main
05-07 08:47:45.572: E/AndroidRuntime(1310): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sudoku/com.example.sudoku.MainActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f030002
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.os.Looper.loop(Looper.java:137)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at java.lang.reflect.Method.invoke(Method.java:511)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at dalvik.system.NativeStart.main(Native Method)
05-07 08:47:45.572: E/AndroidRuntime(1310): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f030002
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.content.res.Resources.getValue(Resources.java:1014)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2139)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.content.res.Resources.getLayout(Resources.java:853)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.app.Activity.setContentView(Activity.java:1881)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at com.example.sudoku.MainActivity.onCreate(MainActivity.java:18)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.app.Activity.performCreate(Activity.java:5104)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-07 08:47:45.572: E/AndroidRuntime(1310):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-07 08:47:45.572: E/AndroidRuntime(1310):     ... 11 more

MainActivity.java

 package com.example.sudoku;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {

       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          // Set up click listeners for all the buttons
          View continueButton = findViewById(R.id.continue_button);
          continueButton.setOnClickListener(this);
          View newButton = findViewById(R.id.new_button);
          newButton.setOnClickListener(this);
          View aboutButton = findViewById(R.id.about_button);
          aboutButton.setOnClickListener(this);
          View exitButton = findViewById(R.id.exit_button);
          exitButton.setOnClickListener(this);
       }
    // ...
       public void onClick(View v) {
       switch (v.getId()) {
       case R.id.about_button:
       Intent i = new Intent(this, about.class);
       startActivity(i);
       break;
       // More buttons go here (if any) ...
       }
       }

    @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;
    }

}

AndroidManifest.xml

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

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

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

   </application>

</manifest>

关于.java

package com.example.sudoku;
import android.app.Activity;
import android.os.Bundle;
public class about extends Activity {
    @Override
    protected void onCreate(Bundle SavedInstanceState){
        super.onCreate(SavedInstanceState);
    setContentView(R.layout.about);
    }


}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@color/background"

android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:padding="30dip"
android:orientation="horizontal">

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="24.5dip"
    android:text="@string/main_title"
    android:textSize="50sp"
    android:textStyle="bold" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_label"
android:id="@+id/continue_label" />
<Button
android:layout_width="fill_parent"
android:id="@+id/new_game_label"
android:layout_height="wrap_content"
android:text="@string/new_game_label" />
<Button
android:layout_width="fill_parent"
android:id="@+id/about_label"
android:layout_height="wrap_content"
android:text="@string/about_label" />
<Button
android:layout_width="fill_parent"
android:id="@+id/exit_label"
android:layout_height="wrap_content"
android:text="@string/exit_label" />

</LinearLayout>
</LinearLayout>
4

3 回答 3

1

确保layout文件main.xml中有一个具有 id的按钮"continue_button"

View continueButton = findViewById(R.id.continue_button);
于 2013-05-07T09:03:55.587 回答
0

你必须使用这种方式。

          Button continueButton = (Button)findViewById(R.id.continue_button);
          continueButton.setOnClickListener(this);

          Button newButton = (Button)findViewById(R.id.new_button);
          newButton.setOnClickListener(this);

          Button aboutButton = (Button)findViewById(R.id.about_button);
          aboutButton.setOnClickListener(this);

          Button exitButton = (Button)findViewById(R.id.exit_button);
          exitButton.setOnClickListener(this);

改变你的main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background"
    android:orientation="horizontal"
    android:padding="30dip" >

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

用你的旧换LinearLayout这个。

于 2013-05-07T09:22:37.107 回答
-1

用 java 文件中的 continue_label 替换 continue_button

编辑:在 java 文件中,您设置了一个名为“main”的布局文件,但实际上它被称为“activity_main”

于 2013-05-07T09:19:56.590 回答