我正在使用一个应用程序,其中显示一个活动3秒钟,然后启动下一个活动。我成功完成了以下代码:
// shows the picture for 3 seconds, then takes the user to GamePlay
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(ShowImage.this, GamePlay.class);
ShowImage.this.startActivity(intent);
ShowImage.this.finish();
}
}, 3000);
此活动还显示图片,我想将图片的ID传递给下一个活动。我知道可以通过使用以下代码(来自经验,在另一个Activity中)完成(我尝试将其插入上面的空行):
this.putExtra("id", selected_image);
但是,我收到错误"方法putExtra(String,int)未定义类型new Runnable(){}"
我的问题是,如何将图片ID传递给下一个活动,同时包含3秒的延迟?
以下是活动代码的其余部分:
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;
import android.widget.Toast;
public class ShowImage extends ActionBarActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.show_image, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// shows the activity
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_image);
// get intent data
Intent intent = getIntent();
// get the selected image id and crop the image
final int selected_image = (Integer) intent.getExtras().getInt("id");
Bitmap [] croppedBitmap = CreateBitmap.createBitmap(this, selected_image, 3);
// create GridView for pictures to be showed in
GridView grid_layout = (GridView) findViewById(R.id.grid_layout);
// pass the array with pieces to the image adapter
grid_layout.setAdapter(new CroppedImageAdapter(this, croppedBitmap));
// test text
Toast.makeText(this, String.valueOf(selected_image), Toast.LENGTH_LONG).show();
// shows the picture for 3 seconds, then takes the user to GamePlay
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(ShowImage.this, GamePlay.class);
this.putExtra("id", selected_image);
ShowImage.this.startActivity(intent);
ShowImage.this.finish();
}
}, 3000);
}
}
答案 0 :(得分:2)
更改
this.putExtra("id", selected_image);
到
intent.putExtra("id", selected_image);
答案 1 :(得分:0)
尝试使用
intent.putExtra("id", selected_image);
而不是:
this.putExtra("id", selected_image);
代码中注意 此是处理程序的引用,您使用意图引用传递意图数据而不是此参考。