EliFromToronto
EliFromToronto

Reputation: 81

Basic problem that I can not solve

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.

Upvotes: 0

Views: 120

Answers (3)

mudit
mudit

Reputation: 25536

I didnt understand what are you trying to accomplish using this architecture... First of all if you are using Class2 only to initialize the layout of MainClass than you should not extend Activity in the Class2.

What i can suggest is this:

package com.s;

import android.app.Activity;
import android.widget.TextView;

public class Class2 {
    TextView tv;
    Activity activity;

    public Class2(Activity activity) {
        this.activity = activity;
    }

    public void Test2() {
        activity.setContentView(R.layout.layoutA);
        TextView tv = (TextView) activity.findViewById(R.id.DisplayLine);
        tv.setText("Start");
    }
}

and for MainClass :

package com.s;

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.main);
        // If this line is enabled, it works fine
        Test1();
        // but if these lines are enabled, you get FORCE CLOSE
        Class2 c2 = new Class2(this);
        c2.Test2();
    }

    public void Test1() {
        setContentView(R.layout.layoutA);
        TextView tv = (TextView) findViewById(R.id.DisplayLine);
        tv.setText("Start");
    }
}

If you are trying to do something different than this, then please come back with what do you actually want.

Upvotes: 0

ASMaitre
ASMaitre

Reputation: 187

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.

Upvotes: 1

Steve
Steve

Reputation: 1449

Class2 c2 = new Class2();
C2.Test2();

Check your capitalization of c2?

Steve

Upvotes: 0

Related Questions