- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做 ImageSwitcher,它工作完美,尝试将 ImageSwitcher 图像发送到另一个 Activity 。 ImageSwitcher 工作完美,但单击按钮转到下一个 Activity 后,错误:Views.java 中的 (imageview) 中出现空指针异常
MainEvent.java
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import java.io.ByteArrayOutputStream;
public class MainEvent extends Activity {
private ImageSwitcher imageSwitcher;
Button btnNext,back,select,refresh;
ImageView imageView;
// Array of Image IDs to Show In ImageSwitcher
int imageIds[]={R.drawable.frame3,R.drawable.frame7,
R.drawable.curtain,R.drawable.potraitimage
};
int messageCount=imageIds.length;
// to keep current Index of ImageID array
int currentIndex=0;
Animation in,out;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_event);
// get The references
btnNext=(Button)findViewById(R.id.button2);
back=(Button)findViewById(R.id.button);
select=(Button)findViewById(R.id.select);
refresh=(Button)findViewById(R.id.refresh);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
// Set the ViewFactory of the ImageSwitcher that will create
ImageView object when asked
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
// TODO Auto-generated method stub
// Create a new ImageView set it's properties
imageView = new ImageView(getApplicationContext());
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setImageResource(R.drawable.potraitimage);
return imageView;
}
});
// Declare the animations and initialize them
in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
// set the animation type to imageSwitcher
imageSwitcher.setInAnimation(in);
imageSwitcher.setOutAnimation(out);
// ClickListener for NEXT button
// When clicked on Button ImageSwitcher will switch between Images
// The current Image will go OUT and next Image will come
in with specified animation
btnNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
imageView.setImageResource(0);
Log.d("index", String.valueOf(currentIndex));
currentIndex++;
Log.d("index", String.valueOf(currentIndex));
// If index reaches maximum reset it
if(currentIndex==messageCount)
currentIndex=0;
imageSwitcher.setImageResource(imageIds[currentIndex]);
}
});
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// set the animation type to imageSwitcher
// TODO Auto-generated method stub
Log.d("index", String.valueOf(currentIndex));
currentIndex--;
Log.d("index", String.valueOf(currentIndex));
// If index reaches maximum reset it
if (currentIndex < 0)
currentIndex = 2;
imageSwitcher.setImageResource(imageIds[currentIndex]);
}
});
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageView.setImageResource(imageIds[currentIndex]);
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Intent in=new Intent(MainEvent.this,Camera.class);
/* in.putExtra("image",bitmap);*/
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Bundle bu=new Bundle();
bu.putByteArray("ImageByte",byteArray );
in.putExtras(bu);
startActivity(in);
}
});
refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onRestart();
}
});
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Intent i = new Intent(MainEvent.this, MainEvent.class); //your class
startActivity(i);
finish();
}
}
View .java
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
public class Views extends AppCompatActivity {
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
image = (ImageView) findViewById(R.id.image);
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("ImageByte");
/* Intent i=getIntent();*/
/* Bitmap bitmap=i.getExtras().getParcelable("image");
image.setImageBitmap(bitmap);*/
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
image.setImageBitmap(bmp);//here null pointer exception
}
}
我不知道如何将图像从图像切换器传递到下一个 Activity 。请帮助我。
最佳答案
不能只传递图像所在的索引(int),而不是传递图像的字节吗?
这样会更有效率。
(来自下面链的扩展答案)在两个 Activity 中共享数组。静态属性的简单方法:
public class StaticModelManager {
public static int imageIds[];
}
然后在应用程序中的任何位置获取或设置它:
StaticModelManager.imageIds = {
R.drawable.frame3,
R.drawable.frame7,
R.drawable.curtain,
R.drawable.potraitimage
};
使用它们:
StaticModelManager.imageIds[currentIndex];
多个 Activity 或 fragment 可以轻松访问这一实例。只要确保在不需要或位于内存中的东西时清理它们即可。
关于java - Android ImageSwitcher 图像到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36199702/
我有一个 ImageSwitcher 和 2 个按钮...“下一个”和“上一个”用于滑动图像...但是它只在一侧从左向右滑动动画...如何解决这个问题?谢谢... Integer[] imageIDs
我正在使用 fade_in 和 fade_out 动画可用 android.R.anim.fade_in(fade_out)。基本上我可以改变动画的持续时间,但无论是 0.5 秒还是 2 秒动画仍然不
我在使用 imageswitcher 以适应屏幕时遇到问题。请检查图片 1。屏幕边缘仍有白色的可用空间。我想实现图2中的效果。没有空白和图像切换器非常适合屏幕。我可以使用 imageView 实现此效
在查看 Android (1.5) SDK 中的 ApiDemos 示例时,有一个使用 ImageSwitcher 的好示例,带有 Gallery 对象以提供“更改图像”操作。 我打算编写的应用程序,
我使用非常简单的代码来显示存储为可绘制对象的图片库。所有图片的大小均小于 200kb,分辨率为 1024x576 像素。 我的代码在我测试的 4 台设备上运行良好,并且适用于我的大多数用户。但是其中一
我正在尝试改进我的 Android 应用程序,我看到 ImageSwitcher 被大量引用用于各种动画任务,但谷歌文档完全是简陋的,除了其中的方法外没有描述任何内容类。同时例子中都使用了galler
我有图像切换器,每 x 秒更改一次图像。 如果我使用 setImageDrawable 将图像设置为切换器,它工作正常。但图像未对齐垂直中心。下面是我的布局
我正在尝试创建一个 ImageSwitcher 来浏览 DCIM 上的相机图像。当我翻阅图像时,出现 OutOfMemory 错误。如果为每个查看的相当大的图像分配内存,那么这是非常有意义的。我研究过
这是一个简单的应用程序,可以通过右/左糖果在图像之间切换 布局: onCreate中的定义: @Override protected void onCreate(Bun
我在 ImageSwitcher 中切换图像时遇到了一些 OutOfMemory 崩溃,这种情况很少见,但有时会发生(对我来说从来没有,否则我可以调试它哈哈)。 我只有 5 张图片 (PNG),每张图
我对 Android 开发非常陌生,我一直在尝试创建一个图片库应用程序。一切工作正常,除了当我滑动以显示下一个或上一个图像时,没有显示任何内容。我该如何解决这个问题? public class Pho
我在调整 primefaces imageSwitch 获取的图像大小时遇到问题。在随附的屏幕截图中,您可以看到我如何为 p:imageSwitch 提供了一个小尺寸的红色边框,但加载图像在外面。
我正在制作一个动画以在 ImageSwitcher 中显示许多图像,但我无法将图像设置为 ImageSwitcher。 这是我的 xml 中的图像切换器。 这是我的
我正在尝试在 android 中构建一个应用程序,在我的一项 Activity 中,我想显示全屏图像并通过在图片上滑动手指使它们左右滑动。 我已经尝试过基本的画廊 View 和图像切换器,但我无法处理
我正在做 ImageSwitcher,它工作完美,尝试将 ImageSwitcher 图像发送到另一个 Activity 。 ImageSwitcher 工作完美,但单击按钮转到下一个 Activit
谁能给我一个没有图库的 ImageSwitcher 的工作示例? 这是我的代码: ImageSwitcher mImageSwitcher; mImageSwitc
我正在尝试实现一个 ImageSwitcher 以移动到下一张图片。 单击“下一步”按钮时效果很好,但我无法让它与 fling GestureDetector 一起使用。 基本上,我在 onFling
我正在使用 Android 的鲜为人知的 ImageSwitcher为 ImageView 中的图像变化设置动画。目前,有 2 个图像(默认和“ok”图像)在某些事件(全部发生在主线程上)上打开 -
我想在 imageSwitcher 类上放置一个计时器。我能够使用以下代码为 2 张图片执行此操作,但我无法添加更多图片。谁能告诉我如何向 ImageSwitcher 计时器添加更多图片? image
我正在尝试在 ImageSwitcher 中显示来自 URL 的图像。我试过: myImageSwitcher.setImageURI(Uri.parse("http://miurl.com/imag
我是一名优秀的程序员,十分优秀!