2011-10-11 58 views
0

我試圖從非活動類開始新活動。Android/Java:從類方法開始活動

從主菜單:

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 


public class Menu extends Activity { 


    Button start, options; 
    GameLoop Game = new GameLoop(); 

    @Override 

    public void onCreate(Bundle mainStart) { 
     super.onCreate(mainStart); 
     setContentView(R.layout.menu); 

    start = (Button) findViewById(R.id.bStart); 

    options = (Button) findViewById(R.id.bOptions); 

    start.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent openStart = new Intent(Menu.this, Game.class); 
      startActivity(openStart); 

     } 
    }); 

    options.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Context mContext = null; //Error called for mContext to be initialized so just tried setting to null. This is most likely the error cause it would make more sense for it to be equal to "getContext()" or something like that 
      Game.Start(mContext);//Here 
     } 
    }); 

    } 

} 

我試圖打開從Game.Start()方法的活動。

import android.content.Context; 
import android.content.Intent; 

public class GameLoop extends Menu{ 
    boolean hello = false; 

    public void Start(Context sContext){ 
     Intent openOptions = new Intent(sContext, Options.class); 
     startActivity(openOptions); 

    } 

} 

我不知道,如果使用上下文將是要對這個正確的方式,但我想這是值得一試。我對java和android完全陌生,所以我幾乎不知道下一步該去哪裏。對於採取什麼方向的任何幫助,我們都會很感激。

+0

首先使您的遊戲類中的靜態啓動方法爲 –

回答

0

Activity擴展了Context,因此您可以在Activity內部使用this

Game.Start(Menu.this); 

我用Menu.this因爲你是內內匿名類(View.OnClickListener)其中this是指這個內部類。

+0

這非常有道理,但它在加載時仍然崩潰? – iRector

+0

什麼是錯誤和哪一行代碼產生它? –

+0

你的'GameLoop'擴展'Menu',你不能用'new'實例化它。 –

0

您是否將新活動添加到了androidmanifest.xml中?