我使用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