当前位置: 代码迷 >> Android >> addView()出错,咋回事啊
  详细解决方案

addView()出错,咋回事啊

热度:68   发布时间:2016-05-01 21:56:23.0
addView()出错,怎么回事啊?
Java code
package my.test;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnTouchListener;import android.view.ViewGroup;import android.widget.LinearLayout;public class TestActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        final DrawView dv = new DrawView(this);        LinearLayout  lin =(LinearLayout)this.findViewById(R.layout.main);        //dv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));        dv.setMinimumWidth(300);        dv.setMinimumHeight(500);        dv.setOnTouchListener(new OnTouchListener(){            public boolean onTouch(View v, MotionEvent event) {                dv.currentX = (event.getX());                dv.currentY = (event.getY());                dv.invalidate();                return true;            }        });        lin.addView(dv);//这里总出错!    }    }




------解决方案--------------------
LinearLayout lin =(LinearLayout)this.findViewById(R.layout.main);//这个不对吧,应该是R.id.xxxxx
你的xml中为LinearLayout定义一个id