2015-04-26 48 views
-1

WelcomeActivity:爲什麼此程序在Android虛擬設備中無法使用?

package com.emdad.androidapp; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.view.WindowManager; 


public class WelcomeActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     findViewById(R.id.androidButton).setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       Intent intent=new Intent(WelcomeActivity.this, GameActivity.class); 
       startActivity(intent); 

      } 
     }); 

     setContentView(R.layout.activity_welcome); 
    } 


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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

GameActivity:

package com.emdad.androidapp; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 

public class GameActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_game); 
    } 

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

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

當它在Android虛擬設備運行時,它回答說: 「不幸的是,它已經停止。」問題是什麼?

回答

2

你應該將

findViewById(R.id.androidButton).setOnClickListener(new OnClickListener() { 

更正

setContentView(R.layout.activity_welcome); 

前:

setContentView(R.layout.activity_welcome); 

findViewById(R.id.androidButton).setOnClickListener(new OnClickListener() { 

    @Override 
    public void onClick(View arg0) { 
     Intent intent=new Intent(WelcomeActivity.this, GameActivity.class); 
     startActivity(intent); 

    } 
});