我在这里遇到一个问题,我需要从数据库中显示数据并以表格的形式在我的应用程序中查看。一切顺利,但由于表格列很长(由于从数据库收到的数据),我决定将方向设置为横向以便于查看(我猜)。实现此代码后,
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
onCreate()函数被调用了两次。我知道它已被调用两次,因为我已经包含了log.i.我的所有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>
答案 0 :(得分:5)
只需添加属性
android:configChanges="keyboardHidden|orientation|screenSize"
标记
<activity>
来源:Android onCreate() Method called twice when device rotated. (ICS)
答案 1 :(得分:2)
in your manifest file
<?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"
android:screenOrientation="landscape" >
</activity>