1

私の問題は、TabActivity の 1 つのタブの下に複数のアクティビティがあることですが、その時点で 1 つのアクティビティから別のアクティビティに 3 回以上移動すると、「java.lang.StackOverflowError」が発生して申し訳ありません。英語でのコミュニケーション。

どんな助けでも大歓迎です。

私のコードは: -

FirstActivity.java

public class FirstActivity extends ActivityGroup implements
        OnClickListener {

    Button mBtnAdd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen1);
        mBtnAdd = (Button) findViewById(R.id.mBtnAddManageAccount);
        mBtnAdd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == mBtnAdd) {
            Intent mInSecond = new Intent(v.getContext(), SecondActivity.class);
            replaceContentView("SecondActivity", mInSecond);
        }
    }

    public void replaceContentView(String id, Intent newIntent) {
        View view = getLocalActivityManager().startActivity(id,
                newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                .getDecorView();
        this.setContentView(view);
    }
}

SecondActivity.java

public class SecondActivity extends ActivityGroup implements
        OnClickListener {

    Button mBtnBack;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);
        mBtnBack=(Button)findViewById(R.id.mBtnBackNewCustomer);
        mBtnBack.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v==mBtnBack){
            Intent mInBack = new Intent(v.getContext(),
                    FirstActivity.class);
            replaceContentView("FirstActivity", mInBack);
        }
    }

    public void replaceContentView(String id, Intent newIntent) {
        View view = getLocalActivityManager().startActivity(id,
                newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                .getDecorView();
        this.setContentView(view);
    }
}

エラー ログキャット:-

07-19 11:02:01.731: E/AndroidRuntime(528): FATAL EXCEPTION: main
07-19 11:02:01.731: E/AndroidRuntime(528): java.lang.StackOverflowError
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6986)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.ScrollView.draw(ScrollView.java:1409)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.View.draw(View.java:6883)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.widget.FrameLayout.draw(FrameLayout.java:357)
07-19 11:02:01.731: E/AndroidRuntime(528):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1862)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewRoot.draw(ViewRoot.java:1522)
07-19 11:02:01.731: E/AndroidRuntime(528):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1258)
07-19 11:02:01.731: E/AndroidRuntime(528):  a
4

3 に答える 3

2

ActivityGroupは実際には非推奨です。代わりにフラグメントを使用する必要があります。これは、スタック オーバーフロー エラーを回避するメモリの管理に役立ちます。

基本的に、最初の項目のように XML でフラグメントを宣言するか、2 番目の項目のようにプレースホルダーを指定できます。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment android:name="com.example.news.ArticleListFragment"
            android:id="@+id/fragment"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
    <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_weight="2"
            android:layout_width="0dp"
            android:layout_height="match_parent" />
</LinearLayout>

これで、最初のフラグメントが自動的に膨張して表示されます。ただし、@id/fragmentContainer はスタブ化しただけなので、インフレートする必要があります。

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

フラグメントを作成するには、Fragment を拡張するだけです (フラグメントを保持するアクティビティが FragmentActivity を拡張していることも確認してください)。次に、ListView 行のビューを作成して返す方法と同様に、表示するビューを作成して返すことを確認します。

public class ExampleFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.example_fragment_layout);

            /* All your find view by id's */
            return view;
    }
}

詳細については、 http ://developer.android.com/guide/components/fragments.html http://developer.android.com/reference/android/app/Fragment.htmlを参照してください。

于 2012-07-22T15:48:18.080 に答える
1

これが私の解決策です

public class ActivityStack extends ActivityGroup {

  private Stack<String> stack;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (stack == null) stack = new Stack<String>();
    //start default activity
    push("FirstStackActivity", new Intent(this, FirstStackActivity.class));
  }

  @Override
  public void finishFromChild(Activity child) {
    pop();
  }

  @Override
  public void onBackPressed() {
    pop();
  }


  public void push(String id, Intent intent) {
    Window window = getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    if (window != null) {
      stack.push(id);
      setContentView(window.getDecorView());
    }
  }

  public void pop() {
    if (stack.size() == 1) finish();
    LocalActivityManager manager = getLocalActivityManager();
    manager.destroyActivity(stack.pop(), true);
    if (stack.size() > 0) {
      Intent lastIntent = manager.getActivity(stack.peek()).getIntent();
      Window newWindow = manager.startActivity(stack.peek(), lastIntent);
      setContentView(newWindow.getDecorView());
    }
  }
}

起動タブ

Intent intent = new Intent().setClass(this, ActivityStack.class);
TabHost.TabSpec spec = tabHost.newTabSpec("tabId")
spec.setContent(intent);

次のアクティビティを呼び出す

public class FirstStackActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView textView = new TextView(this);
    textView.setText("First Stack Activity ");
    textView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Intent intent = new Intent();
          intent.setClass(getParent(), SecondStackActivity .class);
          ActivityStack activityStack = (ActivityStack) getParent();
          activityStack.push("SecondStackActivity", intent);


      }
    });
    setContentView(textView);
  }
}

次をもう一度呼び出す

public class SecondStackActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TextView textView = new TextView(this);
    textView.setText("First Stack Activity ");
    textView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Intent intent = new Intent();
          intent.setClass(getParent(), ThirdStackActivity .class);
          ActivityStack activityStack = (ActivityStack) getParent();
          activityStack.push("ThirdStackActivity", intent);


      }
    });
    setContentView(textView);
  }
}
于 2012-07-24T09:56:51.877 に答える
0

のみFirstActivityから拡張するActivityGroup必要があり、残りはActivityあなたのようなクラスを拡張する必要がありますSecondActivityFirstActivityから起動しないでくださいSecondActivity

このチュートリアルの詳細:

http://www.gamma-point.com/content/android-how-have-multiple-activities-under-single-tab-tabactivity

于 2012-07-21T09:28:44.223 に答える