我在MainActivity中打开一个NewActivity。它可以运行得很好。但是NewActivity中没有布局。它只显示白色。我不知道为什么。
我在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 :(得分:0)
您拼错了onCreate()
onCreat()
@Override
protected void onCreate(Bundle savedInstanceState1){
super.onCreate(savedInstanceState1);
setContentView(R.layout.activity_first);
Log.d("NewActivity", "onCreat");
}
添加e
。考虑使用@Override
注释,极大 helps用于此类事情。