2013-01-21 161 views
0

我在MainActivity中打開一個NewActivity。它可以運行得非常好。但是NewActivity沒有佈局。它只是顯示白色。我不知道爲什麼。onCreate in new Activity can not display

我在R.layout.activity_first中編寫佈局。

MainActivity.java

package com.example.hello_2; 

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

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button button=(Button)this.findViewById(R.id.button1); 
     button.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent intent=new Intent(MainActivity.this,NewActivity.class); 
       startActivity(intent); 
      } 
     }); 
    } 

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

} 

NewActivity.java

package com.example.hello_2; 

import com.example.hello_2.NewActivity; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.SubMenu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.Toast; 

public class NewActivity extends Activity{ 
    protected void onCreat(Bundle savedInstanceState1){ 
     super.onCreate(savedInstanceState1); 
     setContentView(R.layout.activity_first); 
     Log.d("NewActivity", "onCreat"); 
     } 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     return super.onCreateOptionsMenu(menu); 
    } 

} 

回答

0

你拼錯onCreate()onCreat()

@Override 
protected void onCreate(Bundle savedInstanceState1){ 
     super.onCreate(savedInstanceState1); 
     setContentView(R.layout.activity_first); 
     Log.d("NewActivity", "onCreat"); 
     } 

添加e。考慮使用@Override註釋,它爲極大地helps這些事情。

+1

這是[爲什麼我們使用'@ Override'註釋](http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why),kiddies! – Eric

+0

@Eric哇,我甚至都不知道這個問題到現在爲止! SO *確實*有鏈接的一切! –