0

我最近开始了 android app dev,但遇到了我正在尝试制作的应用程序的问题。每当我在模拟器(确切地说是 Android 4.2.2 4.00 屏幕)中加载应用程序时,它只会说“不幸的是,俄罗斯轮盘赌已停止工作。”这是我的主要活动代码:

    package com.sabexus.russianroulette;

import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class MainGameActivity extends Activity{

boolean ReadyToFire;
int Round;
Random Dice = new Random();
int BulletChamber;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_game);
    final TextView HintTxt = (TextView) findViewById(R.id.LabelHint);
    final ImageView barrelImage = (ImageView) findViewById(R.id.Barrel);
    final ImageView barrelSpinningImage = (ImageView) findViewById(R.id.BarrelSpinning);
    final Button TriggerBtn = (Button)findViewById(R.id.trigger);
    final Button ResetBtn = (Button)findViewById(R.id.ResetButton);

    barrelImage.setVisibility(View.VISIBLE);
    barrelSpinningImage.setVisibility(View.GONE);

    //variables
    BulletChamber = 1+Dice.nextInt(6);
    Round = 1; 
    ReadyToFire = false;

    //Trigger button properties.
    TriggerBtn.setVisibility(View.VISIBLE);
    TriggerBtn.setBackgroundColor(Color.TRANSPARENT);

    //Swipe and button touch handlers
    //Trigger Button
    TriggerBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View arg0) {

           }
       });

    //Reset button
    ResetBtn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View arg0) {
               HintTxt.setText("@string/hint");
               Round = 1;
               ResetBtn.setVisibility(View.GONE);
           }
       });

    //gestures   
    barrelImage.setOnTouchListener(new OnSwipeTouchListener() {
           public boolean onSwipeTop() {
               Toast.makeText(MainGameActivity.this, "top", Toast.LENGTH_SHORT).show();
               return true;
           }
           public boolean onSwipeRight() {
               if(Round!=7){
                   if(ReadyToFire == true){
                       barrelImage.setVisibility(View.GONE);
                       barrelSpinningImage.setVisibility(View.VISIBLE);
                   }
               }
               return true;
           }
           public boolean onSwipeLeft() {
               Toast.makeText(MainGameActivity.this, "left", Toast.LENGTH_SHORT).show();
               return true;
           }
           public boolean onSwipeBottom() {
               Toast.makeText(MainGameActivity.this, "bottom", Toast.LENGTH_SHORT).show();
               return 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.main_game, menu);
    return true;
}

};

这是 OnTouchSwipeListener :

package com.sabexus.russianroulette;

import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class OnSwipeTouchListener implements OnTouchListener {

@SuppressWarnings("deprecation")
private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());

public boolean onTouch(final View v, final MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public boolean onDown(MotionEvent e) {
        return super.onDown(e);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        result = onSwipeRight();
                    } else {
                        result = onSwipeLeft();
                    }
                }
            } else {
                if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        result = onSwipeBottom();
                    } else {
                        result = onSwipeTop();
                    }
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public boolean onSwipeRight() {
    return false;
}

public boolean onSwipeLeft() {
    return false;
}

public boolean onSwipeTop() {
    return false;
}

public boolean onSwipeBottom() {
    return false;
}
}

这是我的 Android 清单 XML:

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

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

    <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  >
     <activity
            android:name="com.sabexus.russianroulette.MainGameActivity"
            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>

最后,听到的是我的主要活动 XML:

    <ImageView
        android:id="@+id/Barrel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="67dp"
        android:layout_marginTop="192dp"
        android:src="@drawable/barrel"
        android:contentDescription="@string/desc" />

    <ImageView
        android:id="@+id/BarrelSpinning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="67dp"
        android:layout_marginTop="192dp"
        android:src="@drawable/barrel_spinning"
        android:contentDescription="@string/desc" />

    <TextView
        android:id="@+id/LabelHint"
        android:layout_width="254dp"
        android:layout_height="88dp"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="@string/hint"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/ResetButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/LabelHint"
        android:layout_below="@+id/LabelHint"
        android:text="@string/button"
        android:visibility="gone" />

    <Button
        android:id="@+id/trigger"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/Barrel"
        android:layout_marginBottom="32dp"
        android:layout_marginRight="16dp"
        android:layout_toLeftOf="@+id/Barrel"
        android:text="      " />

</RelativeLayout>

另外,听到的是我的 logcat 日志:

    05-17 17:37:35.744: E/Trace(1094): error opening trace file: No such file or directory (2)
05-17 17:37:35.862: D/AndroidRuntime(1094): Shutting down VM
05-17 17:37:35.862: W/dalvikvm(1094): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-17 17:37:35.884: E/AndroidRuntime(1094): FATAL EXCEPTION: main
05-17 17:37:35.884: E/AndroidRuntime(1094): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sabexus.russianroulette/com.sabexus.russianroulette.MainGameActivity}: java.lang.NullPointerException
05-17 17:37:35.884: E/AndroidRuntime(1094):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at android.os.Looper.loop(Looper.java:137)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at java.lang.reflect.Method.invokeNative(Native Method)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at java.lang.reflect.Method.invoke(Method.java:511)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at dalvik.system.NativeStart.main(Native Method)
05-17 17:37:35.884: E/AndroidRuntime(1094): Caused by: java.lang.NullPointerException
05-17 17:37:35.884: E/AndroidRuntime(1094):     at android.app.Activity.findViewById(Activity.java:1839)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at com.sabexus.russianroulette.MainGameActivity.<init>(MainGameActivity.java:22)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at java.lang.Class.newInstanceImpl(Native Method)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at java.lang.Class.newInstance(Class.java:1319)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-17 17:37:35.884: E/AndroidRuntime(1094):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-17 17:37:35.884: E/AndroidRuntime(1094):     ... 11 more

对不起,很长,noobish post,但我真的不知道它有什么问题:(提前谢谢:D

4

1 回答 1

3

移动此代码

TextView HintTxt = (TextView) findViewById(R.id.LabelHint);
ImageView barrelImage = (ImageView) findViewById(R.id.Barrel);
ImageView barrelSpinningImage = (ImageView) findViewById(R.id.BarrelSpinning);
Button TriggerBtn = (Button)findViewById(R.id.trigger);
Button ResetBtn = (Button)findViewById(R.id.ResetButton);

onCreate之后setContentView

因为您可以在布局膨胀发生后参考视图。SetContentView 是实际发生布局膨胀的地方

于 2013-05-17T17:23:48.150 回答