代码一:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;public class HelloActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
View view = new HelloView(this);
setContentView(view);
}
}代码二:
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.TextView;public class HelloView extends View{
private View HelloView; public HelloView(Context context) {
super(context);
// TODO Auto-generated constructor stub
InitView(context);
} private void InitView(Context context){
HelloView = View.inflate(context, R.layout.main, null);
TextView mText = (TextView)HelloView.findViewById(R.id.text);
Log.i("===mText===","======"+mText);
Log.i("===mText.getText====",""+mText.getText());//这里输出hello world!
}}运行后屏幕就是黑的,不能显示hello world,请问是什么情况?

解决方案 »

  1.   

    setContentView(int layoutResID)
    从这里看知道,这个函数需要的是布局文件的ID,而你给他的参数是View
    setContentView(view);
    应该改为setContentView(R.layout.main);
      

  2.   

    public void setContentView (View view)Since: API Level 1
    Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy. It can itself be a complex view hierarchy. When calling this method, the layout parameters of the specified view are ignored. Both the width and the height of the view are set by default to MATCH_PARENT. To use your own layout parameters, invoke setContentView(android.view.View, android.view.ViewGroup.LayoutParams) instead.文档上有这个方法的啊
      

  3.   

    你在布局文件里mText给值了吗?
    要不你得设,比如mText.setText("Hello");
      

  4.   

    你这样,在第二个类里面添加个函数getHello(){return HelloView ;}
     在第一个类setContentView(view);这里改成这样setContentView(view.getHello());
    这样就行了
      

  5.   

    我在说一句 你这样写是很蛋疼的,在app里自定义view 一般用来自定义某个控件,实现系统没有的效果。
      

  6.   

      你显示的view里面没有内容所以是黑色的,换成那个属性view确实可以了,这样写只能说在设计模式上有需求吧。
      

  7.   


    这个可以,得有返回的视图
    public class HelloActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
         
            View HelloView = View.inflate(this, R.layout.main, null);
    TextView mText = (TextView) HelloView.findViewById(R.id.text);
            setContentView(HelloView);    }