我有6个片段和一个主要活动。当我做Transaction .replace
时我只能用4-6片段来做。如果我替换前3个片段(例如.replace(R.id.Frag3,fragmet)
),当我进行交易时,我的游戏会崩溃。当我用片段3替换片段6时:
@Override
public Fragment getItem ( int arg0){
switch (arg0) {
case 0:
return new Frag1();
case 1:
return new Frag2();
case 2:
return new Frag6();
case 3:
return new Frag4();
case 4:
return new Frag5();
case 5:
return new Frag3();
default:
break;
}
return null;
}
Fragment 3 woks和Fragment 6崩溃所以它是第一个在.replace中崩溃游戏的case
。 PLZ帮助!谢谢。
主要活动:
package com.redot.puzzle3;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends FragmentActivity implements Frag5.F5toF4 ,
Frag5.F5toF6{
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Remove title bar
// this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new
SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
FloatingActionButton fab = (FloatingActionButton)
findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void setF4Riddle(int x){
Frag4 F4 =
(Frag4)getSupportFragmentManager().findFragmentById(R.id.frag4);
if (F4 != null ) {
F4.getF4Riddle(x);
} else {
Frag4 fragment = new Frag4();
Bundle args = new Bundle();
args.putInt("Value", x);
TextView Dialog =(TextView)findViewById(R.id.Dialog);
Dialog.setVisibility(View.GONE);
fragment.setArguments(args);
getSupportFragmentManager().beginTransaction()
.replace(R.id.frag4_container, fragment)
.addToBackStack(null).commit();
fragment.getF4Riddle(x);
}
}
public void setF6Riddle(int x) {
Frag6 F6 = (Frag6)
getSupportFragmentManager().findFragmentById(R.id.frag6);
if (F6 != null) {
F6.getF6Riddle(x);
} else {
Frag6 frag = new Frag6();
Bundle args = new Bundle();
args.putInt("Value", x);
frag.setArguments(args);
TextView dialog =(TextView)findViewById(R.id.dialog);
TextView Q =(TextView)findViewById(R.id.questions);
Button Q1 = (Button) findViewById(R.id.question1);
Button Q2 = (Button) findViewById(R.id.question2);
dialog.setVisibility(View.GONE);
Q.setVisibility(View.GONE);
Q1.setVisibility(View.GONE);
Q2.setVisibility(View.GONE);
getSupportFragmentManager().beginTransaction()
.replace(R.id.frag6_container, frag)
.addToBackStack(null)
.commit();
frag.getF6Riddle(x);
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 6;
}
@Override
public Fragment getItem(int arg0) {
switch (arg0) {
case 0:
return new Frag1();
case 1:
return new Frag2();
case 2:
return new Frag3();
case 3:
return new Frag4();
case 4:
return new Frag5();
case 5:
return new Frag6();
default:
break;
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Inventory";
case 1:
return "3Men";
case 2:
return "Test";
case 3:
return "TheMan";
case 4:
return "King";
case 5:
return "Farmer";
}
return null;
}
}
}
Main_activity布局xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.redot.puzzle3.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
这可能是崩溃的堆栈跟踪:
10-09 00:01:46.985 20690-20690/com.redot.puzzle3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.redot.puzzle3, PID: 20690
java.lang.IllegalArgumentException: No view found for id 0x7f0d0098 (com.redot.puzzle3:id/frag3) for fragment Frag3{128ae004 #5 id=0x7f0d0098}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1326)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1574)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2415)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2200)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2153)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2063)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:725)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6873)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
片段6:
package com.redot.puzzle3;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class Frag6 extends Fragment {
Button startTime;
Button Q1;
Button Q2;
TextView Q;
TextView dialog;
int counter=3;
int questions =0;
int Riddle=1;
boolean Run=true;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myInflatedView = inflater.inflate(R.layout.fragment_frag6, container, false);
dialog =(TextView)myInflatedView.findViewById(R.id.dialog);
Q =(TextView)myInflatedView.findViewById(R.id.questions);
dialog.setText("Do You Have ?");
startTime = (Button) myInflatedView.findViewById(R.id.startTimer);
Q1 = (Button) myInflatedView.findViewById(R.id.question1);
Q2 = (Button) myInflatedView.findViewById(R.id.question2);
Q.setText("Questions: "+ questions +"/5");
askQuestion();
SetRun();
runTimer();
return myInflatedView;
}
private void askQuestion(){
Q1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("FFF", "riddle: "+Riddle);
questions++;
Q.setText("Questions: "+ questions +"/5");
dialog.setText("Answer1");
startTime.setVisibility(View.VISIBLE);
Q1.setVisibility(View.INVISIBLE);
Q2.setVisibility(View.INVISIBLE);
}
});
Q2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
questions++;
Q.setText("Questions: "+ questions +"/5");
dialog.setText("Answer2");
startTime.setVisibility(View.VISIBLE);
Q1.setVisibility(View.INVISIBLE);
Q2.setVisibility(View.INVISIBLE);
}
});
}
private void runTimer() {
startTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (questions == 5) {
dialog.setText("Sorry I cant help any further");
startTime.setVisibility(View.INVISIBLE);
Q1.setVisibility(View.INVISIBLE);
Q2.setVisibility(View.INVISIBLE);
Run=false;
return;
}
startTime.setVisibility(View.INVISIBLE);
new CountDownTimer(1000, 1000) {
public void onTick(long milisec) {
counter--;
dialog.setText("Timer: " + counter);
if(Riddle==2){
}
}
public void onFinish() {
if (questions < 5) {
counter = 3;
dialog.setText("Do You Have Q?");
dialog.setVisibility(View.VISIBLE);
Q1.setVisibility(View.VISIBLE);
Q2.setVisibility(View.VISIBLE);
}
}
}.start();
}
});
}
private void SetRun(){
if(Riddle==2){
questions=0;
Run=true;
}
}
public void getF6Riddle(int x){
Riddle=x;
}
}
片段6布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frag6_container"
tools:context="com.redot.puzzle3.Frag6">
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="784dp"
android:layout_height="1127dp"
android:id="@+id/frag6"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Button
android:id="@+id/question1"
android:layout_width="196dp"
android:layout_height="177dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.176"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Button
android:id="@+id/question2"
android:layout_width="196dp"
android:layout_height="177dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.843"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Button
android:id="@+id/startTimer"
android:layout_width="196dp"
android:layout_height="177dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="Button"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.511"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<TextView
android:id="@+id/dialog"
android:layout_width="593dp"
android:layout_height="318dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:textSize="30sp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.549"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:gravity="center"
app:layout_constraintVertical_bias="0.194"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<TextView
android:id="@+id/questions"
android:layout_width="252dp"
android:layout_height="53dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.163"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.054"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
</android.support.constraint.ConstraintLayout>
片段3:
package com.redot.puzzle3;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Frag3 extends Fragment {
int Riddle;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myInflatedView = inflater.inflate(R.layout.fragment_frag3,
container, false);
// TODO Auto-generated method stub
return myInflatedView;
}
}
片段3布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frag3"
tools:context="com.redot.puzzle3.Frag3">
<!-- TODO: Update blank fragment layout -->
</FrameLayout>