gpt4 book ai didi

android - onCreate 在实现 setRequestedOrientation 后被调用了两次

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:29:16 27 4
gpt4 key购买 nike

我在这里遇到一个问题,我需要显示数据库中的数据并以表格形式在我的应用程序中查看它。一切顺利,但由于表格列很长(由于从数据库接收到的数据),我决定将方向设置为横向以便于查看(我猜)。执行此代码后,

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

onCreate() 函数被调用了两次。我知道它被调用了两次,因为我包含了 log.i。我所有的日志。我被叫了两次。我的日志应该显示这个:

03-19 14:13:53.401: I/loop check(30510): loop
03-19 14:13:54.112: I/jObjLength(30510): 5
03-19 14:13:54.112: I/JSON(30510): jsonObj = work0
03-19 14:13:54.162: I/JSON(30510): jsonObj = work1
03-19 14:13:54.162: I/JSON(30510): jsonObj = work2
03-19 14:13:54.172: I/JSON(30510): jsonObj = work3
03-19 14:13:54.172: I/JSON(30510): jsonObj = work4

但是,它显示的是:

03-19 14:30:12.801: I/loop check(30510): loop
03-19 14:30:12.921: I/jObjLength(30510): 5
03-19 14:30:12.921: I/JSON(30510): jsonObj = work0
03-19 14:30:12.931: I/JSON(30510): jsonObj = work1
03-19 14:30:12.941: I/JSON(30510): jsonObj = work2
03-19 14:30:12.951: I/JSON(30510): jsonObj = work3
03-19 14:30:12.951: I/JSON(30510): jsonObj = work4
03-19 14:30:12.981: I/loop check(30510): loop
03-19 14:30:13.042: I/jObjLength(30510): 5
03-19 14:30:13.042: I/JSON(30510): jsonObj = work0
03-19 14:30:13.042: I/JSON(30510): jsonObj = work1
03-19 14:30:13.052: I/JSON(30510): jsonObj = work2
03-19 14:30:13.052: I/JSON(30510): jsonObj = work3
03-19 14:30:13.062: I/JSON(30510): jsonObj = work4

这发生在我实现 setRequestedOrientation 之后。任何人都可以解释为什么会发生这种情况以及解决方案是什么。谢谢

这是我的 onCreate 类

public void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Log.i("loop check", "loop");
super.onCreate(savedInstanceState);

this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.activity_work_listing);

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);

tblLay = (TableLayout) findViewById(R.id.tblLayout);

jsonParser = new JSONParser();
jObj = jsonParser.getJSONFromUrl(URL);

txtOrderN = new TextView(getApplicationContext());
txtCabinetN = new TextView(getApplicationContext());
txtAssignP = new TextView(getApplicationContext());
txtActualP = new TextView(getApplicationContext());
txtStatS = new TextView(getApplicationContext());

String jsonObj;

for (int i = 0; i < jObj.length(); i++) {
jsonObj = "work";
jsonObj = jsonObj + i;

try {
tblRow = new TableRow(getApplicationContext());
Log.i("JSON", "jsonObj = " + jsonObj);
txtOrderN.setText(jObj.getJSONObject(jsonObj).getString(
"orderN"));
txtCabinetN.setText(jObj.getJSONObject(jsonObj).getString(
"cabinetN"));
txtAssignP.setText(jObj.getJSONObject(jsonObj).getString(
"assignP"));
txtActualP.setText(jObj.getJSONObject(jsonObj).getString(
"actualP"));
txtStatS.setText(jObj.getJSONObject(jsonObj).getString("statS"));
txtOrderN = new TextView(getApplicationContext());
txtCabinetN = new TextView(getApplicationContext());
txtAssignP = new TextView(getApplicationContext());
txtActualP = new TextView(getApplicationContext());
txtStatS = new TextView(getApplicationContext());

tblRow.addView(txtOrderN);
tblRow.addView(txtCabinetN);
tblRow.addView(txtAssignP);
tblRow.addView(txtActualP);
tblRow.addView(txtStatS);
tblLay.addView(tblRow);

} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Error " + e,
Toast.LENGTH_LONG).show();
Log.i("Error ", "" + e);
}

}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zeta.workorder"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.zeta.workorder.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.zeta.workorder.WorkListingActivity"
android:label="@string/title_activity_work_listing" >
</activity>
</application>

</manifest>

最佳答案

只需添加属性
android:configChanges="keyboardHidden|orientation|screenSize"最明显的是 <activity>标签

来源:Android onCreate() Method called twice when device rotated. (ICS)

关于android - onCreate 在实现 setRequestedOrientation 后被调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22498159/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com