I using Eclipse and the Android emulator. Can someone tell me what is wrong here.
// 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>
If Test1 is allowed to run it is OK.
If Test2 is allowed to run, get FORCE CLOSE.
Try instantiate Class2 using startActivity instead Class2 c2 = new Class2();
Also, I’ve noticed that you use c2 and C2 (case sensitive error).
I hope that can help you.