如果是自定义的Layout布局文件的话,别忘了在你调用findViewById()之前在Activity里setContentView(layout.xml)。或者是通过LayoutInflater进行layout.xml的解析。

解决方案 »

  1.   

    是不是xml文件编辑有错误,仔细检查一下,再有如楼上的所说!!!
      

  2.   

    <!--main.xml-->
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <oms.sms.GameView
           android:id="@+id/mainView"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:background="#cfffff00"
           android:textColor="#cf0000ff"
           android:textSize="24sp"
        />
    <TextView
     android:id="@+id/text"
    android:text="@string/hello"
    android:visibility="visible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center_horizontal"
    android:textColor="#ff8888ff"
    android:background="#cfff0000"
    android:textSize="24sp"
         />
         <EditText 
            android:id="@+id/edit"
            android:layout_width="300dip"
            android:layout_height="120dip"
            android:textSize="18sp"
            android:textColor="#cf00ff00"
            android:layout_x="112px"
            android:layout_y="142px"
            android:background="#cf0000ff"
         /> 
         <Button
           android:id="@+id/btnSetText"
           android:textColor="#cdff00ff"
           android:background="#cf00ff00"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" 
           android:text="设置文本"
         /> 
    </FrameLayout>
    <!--end of main.xml-->
    //SMS.java
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.ProgressDialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;public class SMS extends Activity {
        /** Called when the activity is first created. */
    private GameView gameView;
    private TextView text;
    ProgressDialog myDialog=null;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
         //   gameView=(GameView)findViewById(R.id.mainView);
         //   gameView.setTextView((TextView)findViewById(R.id.text));
            text=(TextView)findViewById(R.id.text);
            DisplayMetrics dm=new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            String str="手机分辨率为:\n"+
                        "宽:"+dm.widthPixels+" +高:"+dm.heightPixels+"\n"+
                        dm.widthPixels+" * "+dm.heightPixels;
            text.setText(str);
            EditText edit=(EditText)findViewById(R.id.edit);
            edit.setFocusable(true);
            edit.setAutoTextEnabled(true);
            initButton();
        }
        private void initButton(){
         Button btnSetText=(Button)findViewById(R.id.btnSetText);
         btnSetText.setOnClickListener(new OnClickListener(){
         public void onClick(View view){
          ((Button)view).setText("I'm the one been clicked!");
          text.setText("Ahha,Button had been clicked just now!");
          new AlertDialog.Builder(SMS.this)
          .setTitle("dsfdsdsfdfsfd")
          .setMessage("Message")
          .setPositiveButton("OK", 
           new android.content.DialogInterface.OnClickListener(){
                  public void onClick(DialogInterface it,int i){
                   
                  }
          })
          .show();
           myDialog=ProgressDialog.show(SMS.this, "信息", "程序加载中...");
              new Thread(){
              public void run(){
              try{
              Thread.sleep(3000);
              }catch(InterruptedException ex){
              ex.printStackTrace();
              }
              finally{
              myDialog.dismiss();
              }
              }
              }.start();
         }
         });
        }
    }源码如上所述  我只要把自定义视图GameView在main.xml中的定义给移除掉,结果可以正常运行,否则就是意外终止,不知道事什么原因!!!
    我看这个和例子程序Snake里的完全雷同,不知道那个为什么可以正常运行!!!
      

  3.   

    其中GameView和SMS均定义在包oms.sms中
      

  4.   

    谢谢大家 我知道怎么回事了 我在GameView里只写了一个public GameView(Context context)这个构造函数  然而 XML文件解析必然需要带有AttributeSet这个参数的构造函数  所以在运行时没有构造函数去构造GameView 自然总是出错了