Android的活动启动(由于屏幕方向变化)提供了计算器问题的解决办法不为我工作 [英] Android activity restart(due to screen orientation changes) issue solutions provided on stackoverflow dont work for me

查看:143
本文介绍了Android的活动启动(由于屏幕方向变化)提供了计算器问题的解决办法不为我工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个演示程序来说明我如何处理屏幕方向。这是一个简单的计数器的应用程序,当用户presses增量按钮,反之亦然递增计数。这里是我的code,我的android包括:configChanges = |清单文件keyboardHidden方向,也推翻onConfigurationChanged方法。但不幸的是,我无法preserve计数的值,当我切换模拟器横向计数重置为0,我怎么去了解这个问题呢?

 公共类Counter_demoActivity延伸活动{诠释计数= 0;
TextView的电视;
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    // UIActivity();
    按钮ADD1 =(按钮)findViewById(R.id.btn1);
    按钮SUB1 =(按钮)findViewById(R.id.btn2);
    电视=(的TextView)findViewById(R.id.tv1);
    add1.setOnClickListener(新View.OnClickListener(){
    公共无效的onClick(视图v){
        算上++;
        tv.setText(您Count是+计数);
    }
});
   sub1.setOnClickListener(新View.OnClickListener(){    公共无效的onClick(视图v){
        计数 - ;
        tv.setText(您Count是+计数);
    }
});
}
@覆盖
公共无效onConfigurationChanged(配置NEWCONFIG){
    // TODO自动生成方法存根
    super.onConfigurationChanged(NEWCONFIG);
    的setContentView(R.layout.main);
    // UIActivity();
}
/ *公共无效UIActivity(){
    this.tv =(TextView中)this.findViewById(R.id.tv1);
    this.count = getChangingConfigurations();
} * /

}

我的清单文件:

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=com.arif.counter_demo
安卓版code =1
机器人:=的versionName1.0>
<采用-SDK安卓的minSdkVersion =14/>
<应用
    机器人:图标=@绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    >
    <活动
        机器人:标签=@字符串/ APP_NAME
        机器人:名字=。Counter_demoActivity
        机器人:configChanges =keyboardHidden |方向|键盘>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
< /用途>< /清单>


解决方案

您不会说什么没有工作,但我会冒险猜测。你什么地方得到一个空指针异常。与code的一个问题是,你需要重复大部分您在的onCreate()当你执行 onConfigurationChanged有逻辑。你有一个全新的视图集在活动。否则,该成员字段电视将是无效和不会有连接到你的听众的按钮

修改从您的评论说,计数是不是被preserved,我相信有一些错误的清单和系统正在摧毁和方向的变化重新创建您的活动。以preserve 计数过这样的变化,覆盖 onRetainNonConfigurationInstance()返回包含的值的整数计数,并通过调用 getLastNonConfigurationInstance()的onCreate()的值 >并转换为整数。

I am making a demo app to illustrate how I handle screen orientation. It is a simple counter app that increments the count when the user presses the increment button and viceversa. Here is my code, I included android:configChanges="keyboardHidden|orientation" in manifest file and also overrode onConfigurationChanged method. But unfortunately, I am unable to preserve the value of the count and when I switch emulator to landscape orientation the count is reset to 0, how do I go about this problem?

public class Counter_demoActivity extends Activity {

int count=0;
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //UIActivity();
    Button add1=(Button)findViewById(R.id.btn1);
    Button sub1=(Button)findViewById(R.id.btn2);
    tv=(TextView)findViewById(R.id.tv1);
    add1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        count++;
        tv.setText("Your Count is " + count);
    }
});
   sub1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        count--;
        tv.setText("Your Count is " +count);
    }
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.main);
    //UIActivity();
}
/*public void UIActivity(){
    this.tv=(TextView)this.findViewById(R.id.tv1);
    this.count=getChangingConfigurations();
}*/

}

My manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.arif.counter_demo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" 
    >
    <activity
        android:label="@string/app_name"
        android:name=".Counter_demoActivity"
        android:configChanges="keyboardHidden|orientation|keyboard" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

解决方案

You don't say what didn't work, but I'll venture a guess. You're getting a null pointer exception somewhere. One problem with your code is that you'll need to repeat most of the logic that you have in onCreate() when you execute onConfigurationChanged. You have an entirely new set of views in the activity. Otherwise, the member field tv will be null and there won't be listeners attached to your buttons.

EDIT From your comment that count isn't being preserved, I believe that there's something wrong with your manifest and the system is destroying and re-creating your activity on orientation changes. To preserve count across such changes, override onRetainNonConfigurationInstance() to return an Integer containing the value of count and access that value in onCreate() by calling getLastNonConfigurationInstance() and casting it to an Integer.

这篇关于Android的活动启动(由于屏幕方向变化)提供了计算器问题的解决办法不为我工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆