我是Android编程新手,我在其中一项活动中有以下内容。
我想声明以下内容
AllJokes afrag = new AllJokes();
OneLiners olfrag = new OneLiners();
selectFragment sfrag = new selectFragment();
RaceJokes rfrag = new RaceJokes();
YoMammaJokes yfrag = new YoMammaJokes();
LongJokes lfrag = new LongJokes();
setContentView(R.layout.browse);
在onCreate之外,以便我可以在addalljokes方法中使用它到底部。但是这会导致以下错误。
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.Ayman.jokeit, PID: 3685
java.lang.StackOverflowError
at java.lang.ref.FinalizerReference.<init>(FinalizerReference.java:42)
at java.lang.ref.FinalizerReference.add(FinalizerReference.java:54)
at android.os.StrictMode$InstanceTracker.<init>(StrictMode.java:2244)
at android.os.StrictMode.trackActivity(StrictMode.java:1946)
at android.app.Activity.<init>(Activity.java:760)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:11)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseActivity.<init>(BrowseActivity.java:14)
at com.Ayman.jokeit.selectFragment.<init>(selectFragment.java:14)
at com.Ayman.jokeit.BrowseA
这是活动中的代码
package com.Ayman.jokeit;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
public class BrowseActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AllJokes afrag = new AllJokes();
OneLiners olfrag = new OneLiners();
selectFragment sfrag = new selectFragment();
RaceJokes rfrag = new RaceJokes();
YoMammaJokes yfrag = new YoMammaJokes();
LongJokes lfrag = new LongJokes();
setContentView(R.layout.browse);
//Creats object for fragments
//steps for adding fragment to view
FragmentManager smanager = getFragmentManager();
FragmentTransaction stransaction = smanager.beginTransaction();
stransaction.add(R.id.browse_layout, sfrag, "selectFragment");
stransaction.commit();
//steps for adding fragment to view
FragmentManager olmanager = getFragmentManager();
FragmentTransaction oltransaction = olmanager.beginTransaction();
oltransaction.add(R.id.browse_layout, olfrag, "onelinerFragment");
oltransaction.commit();
}
public void addAllJokes(){
}
}
先谢谢你。
答案 0 :(得分:0)
将其声明,但在onCreate
中启动:
放在onCreate
AllJokes afrag;
OneLiners olfrag;
selectFragment sfrag ;
RaceJokes rfrag;
YoMammaJokes yfrag ;
LongJokes lfrag ;
onCreate
中的
afrag = new AllJokes();
olfrag = new OneLiners();
sfrag = new selectFragment();
rfrag = new RaceJokes();
yfrag = new YoMammaJokes();
lfrag = new LongJokes();