Java 从处理程序内将整数传递给活动
我正在开发一个应用程序,其中一个活动显示3秒钟,然后开始下一个活动。我成功地使用了以下代码:Java 从处理程序内将整数传递给活动,java,android,Java,Android,我正在开发一个应用程序,其中一个活动显示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 in
// 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传递给下一个活动。我知道通过使用下面的代码(我试着在上面的空白行中插入)可以完成(从经验,在另一个活动中):
但是,我得到一个错误“类型new Runnable(){}的方法putExtra(String,int)未定义”
我的问题是,如何在包含3秒延迟的情况下将图像id传递给下一个活动
以下是活动的其余代码:
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);
}
}
改变
this.putExtra("id", selected_image);
到
尝试使用
intent.putExtra("id", selected_image);
而不是:
this.putExtra("id", selected_image);
注意:在您的代码中这是处理程序的引用,您可以使用intent引用传递intent数据,而不是这引用。很有效,谢谢!您可以向我解释一下为什么我做错了,以及我做错了什么吗?您可以通过使用Intent对象添加putExtra将值从一个活动传递到另一个活动。你不能用“这个”。“this”引用当前类。希望这个答案对您有所帮助:)
intent.putExtra("id", selected_image);
this.putExtra("id", selected_image);