毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> 移动平台 >> 正文

findViewbyid()出现空指针

更新时间:2013-5-5:  来源:毕业论文

findViewbyid()出现空指针

我想在MyView类里面给MainActivity里面的textview赋值
public class MyView  extends View {
        TextView txtview;
//LinearLayout linear1;
ListView listv; public MyView(Context context, AttributeSet attrs) {
super(context, attrs);

txtview=(TextView)findViewById(R.id.text1);
if(txtview==null)
System.out.println("txtview is null");
// linear1=(LinearLayout)findViewById(R.id.linear1);
listv=(ListView)findViewById(R.id.listview1);
if(listv==null)
              System.out.println("listv is null");
               //logcat里面显示txtview和listv均为空 }毕业论文 

@SuppressLint("DrawAllocation")
@SuppressWarnings("static-access")
@Override
protected void onDraw(Canvas canvas) {

super.onDraw(canvas);
canvas.drawColor(0x00FFFFFF);
Paint bmpPaint=new Paint();
canvas.drawBitmap(cacheBitmap, 0, 0, bmpPaint);
//canvas.drawPath(path, bmpPaint);
canvas.save(canvas.ALL_SAVE_FLAG);
canvas.restore();
}

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
public boolean onTouchEvent(MotionEvent event) {
int x=(int)event.getX();
int y=(int)event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
flag=true;
startX=x;
startY=y;
loc.clear();
path.moveTo(x,y);

break;
case MotionEvent.ACTION_MOVE:
if(flag)
{
path.moveTo(x,y);
path.lineTo(x,y);
cacheCanvas.drawLine(startX,startY,x,y,paint);
startX=x;
startY=y;
loc.add(new Point(startX,startY));
}

break;
case MotionEvent.ACTION_UP:
   path.reset();
bhnum++;
pic=new int[this.view_height][this.view_width];
for(int i=0;i<pic.length;i++)
for(int j=0;j<pic[i].length;j++)
pic[i][j]=0;
flag=false;
for(int i=0;i<loc.size();i++)
{
Point st=(Point)loc.get(i);
if(st.x>=this.view_width|| st.x<0|| st.y>=this.view_height||st.y<0)
{
continue;
}
int sty=st.y;
int stx=st.x;
pic[sty][stx]=1;
pics[sty][stx]=1;
}
ArrayList aa=new ArrayList();
aa=ljshibie();
Log.i("after ljshibie",aa.toString());
ArrayList bb=new ArrayList();Log.i("afterbb","can excute!");
bb=tjshibie();
Log.i("after tjshibie",bb.toString());
ccs=PutResult(aa,bb);
if( ccs !=null &&ccs.size()>0)
{
CharSequence cs=(String)ccs.get(0);Log.i("匹配字1",cs.toString());
txtview.setText(cs);//给textview赋值 Log.i("匹配字2",cs.toString());
...... }
   break;

}
invalidate();
return true;
}
} context 通常对于的是Activity,比如TestActivity

TestActivity确保如下:
1.
OnCreate()中
   setContentView(R.layout.activity_main);

2.activity_main包含了text1与listview1

3.new MyView(TestActivity.this, null)


在MyView是获取不到txtview,listv
在MainActivity才能获取到txtview,listv。

可以这样:

Java code?public class MainActivity extends Activity {     protected void onCreate(Bundle savedInstanceState)      {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);                   txtview=(TextView)findViewById(R.id.text1);         listv=(ListView)findViewById(R.id.listview1);     }       public void setTextView(String txt)     {         txtview.setText(txt);     } }   public class MyView extends View  {     MainActivity ay;     public MyView(Context context, AttributeSet attrs)      {         super(context, attrs);         ay = (MainActivity) context;         ...     }       public boolean onTouchEvent(MotionEvent event)      {         ...         ay.setTextView("test");         ...     } }

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。