簡體   English   中英

我無法解決的基本問題

[英]Basic problem that I can not solve

我使用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,請強制關閉。

嘗試使用startActivity實例化Class2,而不是Class2 c2 = new Class2();

另外,我注意到您使用c2和C2(區分大小寫的錯誤)。

希望對您有所幫助。

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

檢查您的c2大小寫嗎?

史蒂夫

我不明白您要使用這種體系結構要完成什么...首先,如果您僅使用Class2來初始化MainClass的布局, 則不應該在Class2中擴展Activity

我可以建議的是:

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");
    }
}

對於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");
    }
}

如果您嘗試執行與此不同的操作,請返回您實際想要的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM