无法让我的应用程序工作。只是崩溃了

时间:2013-05-05 23:14:30

标签: java android

我正在努力学习构建Android应用。这是我的第一次尝试。一切都很好,当我想参加这个活动时(这是有人选择摇滚的时候)我可以让它显示Hello World。我试图制定输赢规则,但每当我选择摇滚我的应用程序只是崩溃。请看一下。

package ro.pa.sc;

import java.util.Random;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class Rockvs extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        randombox();
        setContentView(R.layout.activity_rockvs);
        // Show the Up button in the action bar.
        setupActionBar();
    }

    /**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-         vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public  void randombox()
    {
        Random r = new Random();
        int pickedNumber=r.nextInt(2);

        if (pickedNumber==1) {
            win();
        } else if (pickedNumber==2) {
            lose();
        } else if (pickedNumber==0){
            tie();
        }

    }

    public void win() {
        String message = "Computer picked Scissors! You Won!";

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        setContentView(textView);

    }

    public void lose() {

        String message = "Computer picked Paper! You Lose!";

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        setContentView(textView);
    }

    public void tie() {
        String message = "Computer picked Rock! It's a Tie!";

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        setContentView(textView);
    }
}

Logcat输出:

05-05 19:22:46.571: D/ActivityThread(15646): setTargetHeapUtilization:0.25
05-05 19:22:46.571: D/ActivityThread(15646): setTargetHeapIdealFree:8388608
05-05 19:22:46.581: D/ActivityThread(15646): setTargetHeapConcurrentStart:2097152
05-05 19:22:46.722: I/Adreno200-EGL(15646): <qeglDrvAPI_eglInitialize:269>: EGL 1.4 QUALCOMM build:  (CL2803123)
05-05 19:22:46.722: I/Adreno200-EGL(15646): Build Date: 11/28/12 Wed
05-05 19:22:46.722: I/Adreno200-EGL(15646): Local Branch: D1L_JB_1736J_Disable_CPUMempool
05-05 19:22:46.722: I/Adreno200-EGL(15646): Remote Branch: 
05-05 19:22:46.722: I/Adreno200-EGL(15646): Local Patches: 
05-05 19:22:46.722: I/Adreno200-EGL(15646): Reconstruct Branch: 
05-05 19:22:48.233: W/dalvikvm(15646): threadid=1: thread exiting with uncaught exception (group=0x4126c438)
05-05 19:22:48.253: E/AndroidRuntime(15646): FATAL EXCEPTION: main
05-05 19:22:48.253: E/AndroidRuntime(15646): java.lang.RuntimeException: Unable to start activity ComponentInfo{ro.pa.sc/ro.pa.sc.Rockvs}: android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2080)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.app.ActivityThread.access$600(ActivityThread.java:137)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.os.Looper.loop(Looper.java:137)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.app.ActivityThread.main(ActivityThread.java:4904)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at java.lang.reflect.Method.invokeNative(Native Method)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at java.lang.reflect.Method.invoke(Method.java:511)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at dalvik.system.NativeStart.main(Native Method)
05-05 19:22:48.253: E/AndroidRuntime(15646): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class <unknown>
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:321)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.app.Activity.setContentView(Activity.java:2087)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at ro.pa.sc.Rockvs.onCreate(Rockvs.java:20)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.app.Activity.performCreate(Activity.java:5232)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2044)
05-05 19:22:48.253: E/AndroidRuntime(15646):    ... 11 more
05-05 19:22:48.253: E/AndroidRuntime(15646): Caused by: java.lang.reflect.InvocationTargetException
05-05 19:22:48.253: E/AndroidRuntime(15646):    at java.lang.reflect.Constructor.constructNative(Native Method)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
05-05 19:22:48.253: E/AndroidRuntime(15646):    ... 24 more
05-05 19:22:48.253: E/AndroidRuntime(15646): Caused by: android.content.res.Resources$NotFoundException: File You picked Rock! I hope you win! from drawable resource ID #0x7f050009: .xml extension required
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.content.res.Resources.loadColorStateList(Resources.java:2094)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.content.res.TypedArray.getColorStateList(TypedArray.java:342)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.widget.TextView.<init>(TextView.java:820)
05-05 19:22:48.253: E/AndroidRuntime(15646):    at android.widget.TextView.<init>(TextView.java:489)
05-05 19:22:48.253: E/AndroidRuntime(15646):    ... 27 more

XML文件:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Rockvs" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/pickedrock"
        android:textColor="@string/pickedrock"
        android:textSize="25sp" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:3)

在您的XML中,行:

android:textColor="@string/pickedrock"

不正确 - 它应该指向颜色资源,而不是字符串。