尝试在这里学习som android并且需要解决片段到片段的通信问题。它对我来说是香蕉,因为似乎是一个递归的叫声而崩溃了。它似乎发生在FragmentA.onCreateView中。请帮忙..我没有任何线索!
错误:
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at no.clausjensen.fragmentcommunication.FragmentA.onCreateView(FragmentA.java:23)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.app.Fragment.performCreateView(Fragment.java:2053)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:870)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1045)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1147)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2116)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.app.Activity.onCreateView(Activity.java:5282)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.app.Activity.setContentView(Activity.java:2144)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at no.clausjensen.fragmentcommunication.MainActivity.onCreate(MainActivity.java:22)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.app.Activity.performCreate(Activity.java:5933)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.app.ActivityThread.access$800(ActivityThread.java:144)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.os.Looper.loop(Looper.java:135)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at android.app.ActivityThread.main(ActivityThread.java:5221)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at java.lang.reflect.Method.invoke!(Native method)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at java.lang.reflect.Method.invoke(Method.java:372)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-05 00:13:59.139 2238-2238/no.clausjensen.fragmentcommunication E/art﹕ [ 03-05 00:13:59.183 2238: 2238 D/AndroidRuntime ]
Shutting down VM
--------- beginning of crash
03-05 00:13:59.183 2238-2238/no.clausjensen.fragmentcommunication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: no.clausjensen.fragmentcommunication, PID: 2238
java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack available
03-05 00:13:59.421 2238-2250/no.clausjensen.fragmentcommunication I/art﹕ Clamp target GC heap from 67MB to 64MB
03-05 00:13:59.421 2238-2250/no.clausjensen.fragmentcommunication I/art﹕ Background partial concurrent mark sweep GC freed 9773(360KB) AllocSpace objects, 1(12KB) LOS objects, 0% free, 63MB/64MB, paused 559us total 201.614ms
这是我的代码:
package no.clausjensen.fragmentcommunication;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by nr1 on 04/03/15.
*/
public class FragmentA extends Fragment implements View.OnClickListener {
Button btnClick;
int counter = 0;
Communicator comm;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
//return inflater.inflate(R.layout.fragment_a, container, false);
Log.d("FragmentA: ","onCreateView" );
View rootView = inflater.inflate(R.layout.fragment_a, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d("FragmentA: ","onActivityCreated" );
//det finnes ikke metode for å finne byId i fragment, må man bruke activity.findView...byId
comm=(Communicator) getActivity();
btnClick = (Button) getActivity().findViewById(R.id.btn1);
btnClick.setOnClickListener(this);
}
@Override
public void onClick(View view){
Log.d("FragmentA: ","onClick" );
counter++;
comm.respond("The button was clicked " + counter + " times");
}
}
更多代码:
package no.clausjensen.fragmentcommunication;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by nr1 on 04/03/15.
*/
public class FragmentB extends Fragment {
TextView txt1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_b, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
txt1=(TextView) getActivity().findViewById(R.id.txwText);
}
public void changeText(String txtVar){
txt1.setText(txtVar);
}
}
然后是一些:
package no.clausjensen.fragmentcommunication;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
public class MainActivity extends Activity implements Communicator {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void respond(String data){
FragmentManager fm = getFragmentManager();
FragmentB frg2 = (FragmentB) fm.findFragmentById(R.id.fragment2);
frg2.changeText(data);
}
}
界面:
package no.clausjensen.fragmentcommunication;
/**
* Created by nr1 on 04/03/15.
*/
public interface Communicator {
public void respond (String data);
}
答案 0 :(得分:1)
我很确定stacktrace你在layoutB中添加了fragmentA,在布局A中添加了fragmentB或类似的东西
答案 1 :(得分:0)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#99cc00">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:id="@+id/btn1" />
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_a"
android:name="no.clausjensen.fragmentcommunication.FragmentA"/>
</LinearLayout>
那么当然它会尝试在fragment_a ......等内部创建一个新的fragment_a! NOOBME!