2013-05-17 46 views
0

我最近啓動了Android應用程序開發,但遇到了我試圖做的應用程序的問題。每當我加載應用程序在模擬器(安卓4.2.2 4.00" 屏幕是精確的)它只是說,‘不幸的是,俄羅斯輪盤已停止工作’ 這是我的主要活動代碼:Android開發 - 應用程序停止工作,代碼中沒有錯誤

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職位,但我真的不能找出什麼毛病:(在此先感謝:d

回答

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); 

onCreatesetContentView後。

因爲您可以在佈局通貨膨脹發生後引用視圖。 SetContentView實際上是佈局通貨膨脹發生的地方

+0

沒有工作:(仍然得到錯誤,還必須在所有佈局聲明之前添加前綴'final',所以也許這又打破了它? –

+1

@AlexS更新你的問題與新的logcat跟蹤帖子你的logcat中的紅線表明問題 – Pragnani

+0

更新,感謝您幫助我解決這個問題:) –

相關問題