我使用Eclipse和Android模擬器。有人能告訴我這裏有什麼問題嗎?我無法解決的基本問題
// FILE MainClass.java
package xxx.yyy;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainClass extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutA);
// If this line is enabled, it works fine
Test1();
// but if these lines are enabled, you get FORCE CLOSE
Class2 c2 = new Class2();
C2.Test2();
}
public void Test1() {
setContentView(R.layout.layoutA);
TextView tv = (TextView)findViewById(R.id.DisplayLine);
tv.setText("Start");
}
}
// FILE Class2.java
package xxx.yyy;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Class2 extends Activity {
TextView tv;
// @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void Test2() {
setContentView(R.layout.layoutA);
TextView tv = (TextView)findViewById(R.id.DisplayLine);
tv.setText("Start");
}
}
// FILE layoutA.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:id="@+id/DisplayLine"
android:layout_width="350px"
android:layout_height="40px"
android:background="#ff99ff99"
android:textStyle="bold"
android:textColor="#ff000000"
android:layout_x="10px"
android:layout_y="10px"
>
</TextView>
</AbsoluteLayout>
如果Test1的允許運行正常。 如果允許Test2運行,則獲取FORCE CLOSE。
我不認爲你可以像在Java中一樣使用對象初始化來開始新的活動。無論哪種方式,將作爲@ASMaitre建議。 – springrolls 2011-03-25 13:53:45
打開你的logcat,然後按下帶有E字母的圓圈。然後找出哪一行會爲您的代碼帶來錯誤 – Fugogugo 2011-03-25 17:51:02
正如Theresia所建議的那樣,您對Android缺乏瞭解。手機上的開發很難,通過Android學習Java更困難。我建議先學習Java,然後轉向Android。 – Kheldar 2012-01-27 13:34:45