English 中文(简体)
蜂窝活动重新启动(由于筛选方向变化)
原标题:Android activity restart(due to screen orientation changes) issue solutions provided on stackoverflow dont work for me

我正在做一个词汇,以说明我如何处理屏幕定位。 简单的反省是,当用户要求加固顿语和反面语时,就增加了计算量。 我的守则包括以下文字:混凝土机=“键盘板H iddentoration”,也转载于ConfigurationChanged方法。 但不幸的是,我无法保持计票的价值,当我把 em子转向地貌景观时,我是如何处理这个问题的?

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);
    iii
iii);
   sub1.setOnClickListener(new View.OnClickListener() {

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

iii

我的明确档案:

<?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.

http://www.ohchr.org。 根据您的评论,count 我认为,你的发言有点错,而制度正在破坏和重新激发你改变方向的活动。 为保存<条码>>在此种改动中的条目/代码>,上下限<代码>,以返还含有<条码>>的值/打字/代码>的Integer,以及<条码>onCreate (中的这一值,请上LastNonConfiguration Instance()并将其列入Integer。

问题回答

Why you are Overriding onConfigurationChanged while you don t have need to do this...in your case.don t remove android:configChanges="keyboardHidden|orientation" in manifest file just remove the code

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
//UIActivity();
}

页: 1 清理和重建......

取消这种方法

@Override
public void onConfigurationChanged(Configuration newConfig) {
      ....
}

并明确表明:

甲状腺炎和甲状腺炎

反常或反常?

正义 下面的行文和检查您的活动是否正在恢复? 在你的活动中添加一条线,以创造方法,然后改变方向。 底线是否出现在标识中?

甲型六氯环己烷

在我有同一问题时,我在博客中看到了一个trick。 Try it

1. 创立一种方法,以弥补缺陷

private void getsavedstuff() {
    Object mObject  = getLastNonConfigurationInstance();
}

and use it right after setContentView

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getsavedstuff();
}

Now here is a solution that makes sense:)

public class Counter_demoActivity extends Activity {
final private static String COUNT_TITLE = "title";
private int restoredCount;
int count;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    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
protected void onSaveInstanceState(Bundle outState) {
    outState.putInt(COUNT_TITLE, count);
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
     restoredCount =  savedInstanceState.getInt(COUNT_TITLE);
     count=restoredCount;
                 tv.setText("Your Count is " + restoredCount);
    super.onRestoreInstanceState(savedInstanceState);
}

}

The problem is that you are calling setContentView inside your onConfigurationChanged.

If you overwrite onConfigurationChanged and add android:configChanges="keyboard|keyboardHidden|orientation" your activity will not be destroyed and recreated, instead onConfigurationChanged will be called. If you do not implement it super.onConfigurationChanged is called. The config changed method of the Superclass will rotate your layout without removing the values from all the fields etc. In your case the layout gets rotated correctly and after that you reset the layout yourself with calling setContentView(R.layout.main);





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签