[英]Is there a solution that access MainActivity from other Activity class?
[英]Access from class MainActivity to variable from other class
在 class MainActivity 中,我无权访问 gamePanel 变量或对象。 从程序后面的其他班级开始,它运作良好。 我收到以下错误:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.camerademo3/com.example.camerademo3.MainActivity}:
Attempt to read from field 'java.util.ArrayList com.example.camerademo3.GamePanel.images2' on a null object reference
Class 主要活动:
public class MainActivity extends Activity {
GamePanel gamePanel;
@Override
protected void onCreate(Bundle savedInstanceState) {
//some code
setContentView(new GamePanel(this));
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 100);
//some code
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
//some code
if (requestCode == 100) {
Bitmap captureImage = (Bitmap) data.getExtras().get("data");
gamePanel.images.add(captureImage); // no access here
}
//some code
}
}
Class 游戏面板:
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {
//some code
protected ArrayList<Bitmap> images;
public GamePanel(Context context) {
super(context);
images = new ArrayList<>();
}
//some code
}
您不会将 GamePanel 存储在任何地方。 我想你想做
gamePanel = new GamePanel(this)
setContentView(gamePanel)
否则,当您引用 gamePanel 时,它将是 null,因为您从未设置过它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.