android – 定向更改后设置imageview

我有以下代码,这是正常工作,直到我旋转我的手机.然后我必须再次单击加载图像,我明白当我们旋转活动重新启动,我们有一些方法来存储状态并恢复它,但在我的情况下,您可以看到img文件是一个字符串它是随机生成的.
那么我怎么可以使用onConfigurationChanged(这似乎很容易理解)在旋转之前恢复以前的图像?

public class HomeScreen extends Activity {

    protected ImageView imgView;
    protected String str;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);
        final Random rnd = new Random();
        ActionBar actionBar = getActionBar();
        actionBar.hide();

        imgView = (ImageView) findViewById(R.id.imgRandom);

        if (savedInstanceState != null) {
            str = savedInstanceState.getString("param");
            imgView.setImageDrawable
                    (
                            getResources().getDrawable(getResourceID(str, "drawable", getApplicationContext()))
                    );




            imgView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {



                   // final ImageView img = (ImageView) findViewById(R.id.imgRandom);
                    // I have 3 images named img_0 to img_2, so...
                    str = "img" + rnd.nextInt(9);
                    imgView.setImageDrawable
                            (
                                    getResources().getDrawable(getResourceID(str, "drawable", getApplicationContext())));


                }


            });


        }
    }

    protected void onSaveInstanceState(Bundle savedInstanceStat) {
        super.onSaveInstanceState(savedInstanceStat);
        savedInstanceStat.putString("param", str);
    }

当我旋转它是崩溃,也在加载或onClick没有图像正在加载..

> 07-07 21:11:32.950: I/InputReader(468): Device reconfigured: id=1,
> name='Genymotion Virtual Input', size 1080x1920, orientation 0, mode
> 1, display id 0 07-07 21:11:32.950: I/ActivityManager(468): Config
> changes=480 {1.0 310mcc260mnc en_US ?layoutDir sw360dp w360dp h567dp
> 480dpi nrml port finger qwerty/v/v dpad/v s.14} 07-07 21:11:33.081:
> W/ResourceType(1861): Too many attribute references, stopped at:
> 0x01010034 07-07 21:11:33.081: W/ResourceType(1861): Too many
> attribute references, stopped at: 0x01010034 07-07 21:11:33.082:
> D/AndroidRuntime(1861): Shutting down VM 07-07 21:11:33.083:
> E/AndroidRuntime(1861): FATAL EXCEPTION: main 07-07 21:11:33.083:
> E/AndroidRuntime(1861): Process:
> app.motivation.techiequickie.ypb.motivation, PID: 1861 07-07
> 21:11:33.083: E/AndroidRuntime(1861): java.lang.RuntimeException:
> Unable to start activity
> ComponentInfo{app.motivation.techiequickie.ypb.motivation/app.motivation.techiequickie.ypb.motivation.HomeScreen}:
> java.lang.NullPointerException: name is null 07-07 21:11:33.083:
> E/AndroidRuntime(1861):   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
> 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
> 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
> android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3947)
> 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
> android.app.ActivityThread.access$900(ActivityThread.java:151) 07-07
> 21:11:33.083: E/AndroidRuntime(1861):     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
> 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
> android.os.Handler.dispatchMessage(Handler.java:102) 07-07
> 21:11:33.083: E/AndroidRuntime(1861):     at
> android.os.Looper.loop(Looper.java:135) 07-07 21:11:33.083:
> E/AndroidRuntime(1861):   at
> android.app.ActivityThread.main(ActivityThread.java:5254) 07-07
> 21:11:33.083: E/AndroidRuntime(1861):     at
> java.lang.reflect.Method.invoke(Native Method) 07-07 21:11:33.083:
> E/AndroidRuntime(1861):   at
> java.lang.reflect.Method.invoke(Method.java:372) 07-07 21:11:33.083:
> E/AndroidRuntime(1861):   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
> 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 07-07
> 21:11:33.083: E/AndroidRuntime(1861): Caused by:
> java.lang.NullPointerException: name is null 07-07 21:11:33.083:
> E/AndroidRuntime(1861):   at
> android.content.res.Resources.getIdentifier(Resources.java:2034) 07-07
> 21:11:33.083: E/AndroidRuntime(1861):     at
> app.motivation.techiequickie.ypb.motivation.HomeScreen.getResourceID(HomeScreen.java:148)
> 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
> app.motivation.techiequickie.ypb.motivation.HomeScreen.onCreate(HomeScreen.java:35)
> 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
> android.app.Activity.performCreate(Activity.java:5990) 07-07
> 21:11:33.083: E/AndroidRuntime(1861):     at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
> 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
> 07-07 21:11:33.083: E/AndroidRuntime(1861):   ... 11 more 07-07
> 21:11:33.084: W/ActivityManager(468):   Force finishing activity 1
> app.motivation.techiequickie.ypb.motivation/.HomeScreen
看起来主要的问题是你的imgView.setOnClickListener()的调用嵌套在你的if(savedInstanceState!= null)的情况下.

我通过解除嵌套点击监听器的方式使您的代码工作,并向资源名称添加下划线.我使用了九个图像,通过img_8.png命名为img_0.png,然后放在drawable文件夹中.

我还添加了在启动时加载随机映像的代码.

之后,它成功地在点击上加载了另一个随机图像.我也测试了屏幕旋转,现有的代码工作正常,它保持当前图像呈现,无论我旋转屏幕多少次.

这是完全工作和测试的代码:

public class HomeScreen extends Activity {

    protected ImageView imgView;
    protected String str;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);
        final Random rnd = new Random();
        //ActionBar actionBar = getActionBar();
        //actionBar.hide();

        imgView = (ImageView) findViewById(R.id.imgRandom);


        if (savedInstanceState != null) {
            str = savedInstanceState.getString("param");
            Log.d("image screen rotation", str);
            imgView.setImageDrawable
                    (
                            getResources()
                            .getDrawable(getResourceID(str, "drawable", getApplicationContext()))
                    );
        }
        else{
            str = "img_" + rnd.nextInt(9);
            Log.d("image startup", str);
            imgView.setImageDrawable(
                    getResources()
                            .getDrawable(getResourceID(str, "drawable", getApplicationContext())));
        }

        imgView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                // final ImageView img = (ImageView) findViewById(R.id.imgRandom);
                // I have 3 images named img_0 to img_2, so...
                str = "img_" + rnd.nextInt(9);
                Log.d("image click", str);
                imgView.setImageDrawable(
                         getResources()
                         .getDrawable(getResourceID(str, "drawable", getApplicationContext())));

            }
        });


    }

    protected final static int getResourceID
            (final String resName, final String resType, final Context ctx)
    {
        final int ResourceID =
                ctx.getResources().getIdentifier(resName, resType,
                        ctx.getApplicationInfo().packageName);
        if (ResourceID == 0)
        {
            throw new IllegalArgumentException
                    (
                            "No resource string found with name " + resName
                    );
        }
        else
        {
            return ResourceID;
        }
    }


    @Override
    protected void onSaveInstanceState(Bundle savedInstanceStat) {
        super.onSaveInstanceState(savedInstanceStat);
        savedInstanceStat.putString("param", str);
    }
}

运行应用程序的结果日志,单击ImageView更改图像,然后旋转屏幕:

 D/image startup﹕ img_8
 D/image screen rotation﹕ img_8
 D/image screen rotation﹕ img_8
 D/image click﹕ img_3
 D/image screen rotation﹕ img_3
 D/image screen rotation﹕ img_3
 D/image screen rotation﹕ img_3
 D/image screen rotation﹕ img_3
 D/image click﹕ img_7
 D/image screen rotation﹕ img_7
 D/image screen rotation﹕ img_7
 D/image screen rotation﹕ img_7
 D/image screen rotation﹕ img_7
 D/image screen rotation﹕ img_7
 D/image screen rotation﹕ img_7
翻译自:https://stackoverflow.com/questions/31228330/set-imageview-after-orientation-change

转载注明原文:android – 定向更改后设置imageview