2012-07-02 56 views
0

我剛開始在這個應用程序上工作我覺得我已經做好了一切(這不是我的第一個應用程序)但是當我嘗試運行它時它崩潰?它給了我沒有錯誤。Android應用程序在主要活動啓動時發生崩潰

下面是主要活動的代碼,然後是manufest,然後是xml佈局。 `

 package com.theory.game; 
    import java.util.Random; 
    import android.app.Activity; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 

    public class GameTheoryActivity extends Activity implements View.OnClickListener{ 

    Button play, settings; 
    int i; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     play.setOnClickListener(this); 
     settings.setOnClickListener(this); 

    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     switch(v.getId()){ 
     case R.id.bPlay: 

      Random irand = new Random(); 
      i = irand.nextInt(4); 


      switch(i){ 
      case 0: 

       Intent GoMillionair = new Intent(GameTheoryActivity.this, millionair.class); 
       startActivity(GoMillionair); 
       return; 
      case 1: 
       return; 
      case 2: 

       return; 
      case 3: 

       return; 
      } 

      return; 
     case R.id.bSettings: 
      return; 
     } 
    } 
} 

Manufest:

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

    <uses-sdk android:minSdkVersion="15" /> 

    <application > 
     <activity 
      android:name=".GameTheoryActivity" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".millionair" > 
      <intent-filter> 
       <action android:name="com.theory.game.MILLIONAIR" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/backgroundimage" 
    android:orientation="vertical" > 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="390dp" 
     android:orientation="vertical" > 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 


     <Button 
      android:id="@+id/bPlay" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.38" 
      android:background="@drawable/play" /> 

     <Button 
      android:id="@+id/bSettings" 
      android:layout_width="match_parent" 
      android:layout_height="58dp" 
      android:layout_weight="0.36" 
      android:background="@drawable/settings" /> 
    </LinearLayout> 

</LinearLayout> 

`

而且我還有一個類叫做millionair這僅僅是一個普通的類沒什麼,沒什麼我錯了。請檢查這個怎麼回事,讓我知道爲什麼我的應用程序不會開始說它「已停止工作」..謝謝

+4

它給你一個錯誤。檢查LogCat並在此處發佈堆棧跟蹤。 –

回答

1

我的猜測是你得到一個NullPointerException,因爲playsettings對象都爲空。

改變你的onCreate()看起來像這樣:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    play = (Button)findViewById(R.id.bPlay); 
    settings = (Button)findViewById(R.id.bSettings); 

    play.setOnClickListener(this); 
    settings.setOnClickListener(this); 

} 
+0

是的多數民衆贊成在此,謝謝你們:D –

0

貌似播放和設置都將是空當您嘗試在onCreate()方法中使用,則:

play.setOnClickListener(this); 
settings.setOnClickListener(this); 

確保他們開始像這樣:

play = (Button)findViewById(R.id.bPlay); 
settings = (Button)findViewById(R.id.bSettings) 
play.setOnClickListener(this); 
settings.setOnClickListener(this); 
+0

謝謝修復它:D –

1

你可能會得到一個NullPointerException因爲您忘記將按鈕分配給您的字段(Button play, settings;)。

下面是關於如何分配他們的例子:

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    play = (Button) findViewById(R.id.bPlay); 
    settings = (Button) findViewById(R.id.bSettings); 
} 
+0

啊是的這就是爲什麼!我是怎麼忘記這一步的:D 謝謝男人:) –