我被困了,任何人都可以帮助我。我希望在Android开发中使图像按钮随时间不断移动。 每当我尝试运行它时,我的应用程序都会崩溃
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Timer timer=new Timer();
class UpdatePosition extends TimerTask{
ImageButton move=(ImageButton) findViewById(R.id.car);
public void run(){
RelativeLayout.LayoutParams mParams=(RelativeLayout.LayoutParams) move.getLayoutParams();
mParams.leftMargin += 1;
move.setLayoutParams(mParams);
}
}
TimerTask UpdateImage =new UpdatePosition();
timer.scheduleAtFixedRate(UpdateImage,0,1000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
提前谢谢