2016-10-02 51 views
0

我是Android新手。 我在倒数计时器上移动两个片段。 10秒后下一个片段应该出现,但我的应用程序停止工作。 我尝试了一些解决方案,但无法找到它。 请帮帮我。应用停止使用倒数计时器的片段

Question3.java(第一个片段)

package com.example.sheikhspc.testapp; 


import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.app.Fragment; 
import android.os.CountDownTimer; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 


public class Question3 extends Fragment { 


public Question3() { 
    // Required empty public constructor 
} 
CountDownTimer timer; 
int score; 
Fragment frag; 
TextView tv, scr; 
View view; 
ImageView imageView1, imageViewR, imageView2; 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    view = inflater.inflate(R.layout.fragment_question3, container, false); 

    tv = (TextView)view.findViewById(R.id.timer3); 
    imageView1 = (ImageView) view.findViewById(R.id.wrong1); 
    imageViewR = (ImageView) view.findViewById(R.id.right); 
    imageView2 = (ImageView) view.findViewById(R.id.wrong2); 


    timer = new CountDownTimer(10000, 1000) { 

     public void onTick(long millisUntilFinished) { 
      tv.setText("seconds remaining: " + millisUntilFinished/1000); 
      //here you can have your logic to set text to edittext 
     } 

     public void onFinish() { 
      tv.setText("done!"); 
      frag = new Question4(); 
      FragmentManager fm = getFragmentManager(); 
      FragmentTransaction fragmentTransaction = fm.beginTransaction(); 

      fragmentTransaction.replace(R.id.container, frag); 

      fragmentTransaction.commit(); 

     } 

    }; 
    timer.start(); 


    return view; 
} 

} 

Question4.java(第二片段)

package com.example.sheikhspc.testapp; 


import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.os.Bundle; 
import android.app.Fragment; 
import android.os.CountDownTimer; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 


public class Question4 extends Fragment implements View.OnClickListener { 


public Question4() { 
    // Required empty public constructor 
} 
Fragment frag; 
TextView tv; 
View view1; 
CountDownTimer timer; 
ImageView imageView1, imageViewR, imageView2; 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    view1 = inflater.inflate(R.layout.fragment_question4, container, false); 

    tv = (TextView)view1.findViewById(R.id.timer4); 
    imageView1 = (ImageView) view1.findViewById(R.id.wrong1); 
    imageViewR = (ImageView) view1.findViewById(R.id.right); 
    imageView2 = (ImageView) view1.findViewById(R.id.wrong2); 
    imageView1.setOnClickListener(this); 
    imageViewR.setOnClickListener(this); 
    imageView2.setOnClickListener(this); 

    timer = new CountDownTimer(10000, 1000) { 

     public void onTick(long millisUntilFinished) { 
      tv.setText("seconds remaining: " + millisUntilFinished/1000); 
      //here you can have your logic to set text to edittext 
     } 

     public void onFinish() { 
      tv.setText("done!"); 
      frag = new Question5(); 
      FragmentManager fm = getFragmentManager(); 
      FragmentTransaction fragmentTransaction = fm.beginTransaction(); 

      fragmentTransaction.replace(R.id.container, frag); 

      fragmentTransaction.commit(); 

     } 

    }; 
    timer.start(); 




    return view1; 
} 

@Override 
public void onClick(View v) { 
} 
} 

错误日志

E/art: Throwing OutOfMemoryError "Failed to allocate a 12695052 byte allocation with 7472568 free bytes and 7MB until OOM" 
D/skia: --- allocation failed for scaled bitmap 
D/AndroidRuntime: Shutting down VM 
E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: com.example.sheikhspc.testapp, PID: 21504 
       java.lang.OutOfMemoryError: Failed to allocate a 12695052 byte allocation with 7472568 free bytes and 7MB until OOM 
        at dalvik.system.VMRuntime.newNonMovableArray(Native Method) 
        at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
        at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609) 
        at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) 
        at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988) 
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2477) 
        at android.content.res.Resources.loadDrawable(Resources.java:2384) 
        at android.content.res.TypedArray.getDrawable(TypedArray.java:749) 
        at android.view.View.<init>(View.java:3742) 
        at android.widget.ImageView.<init>(ImageView.java:149) 
        at android.widget.ImageView.<init>(ImageView.java:145) 
        at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:57) 
        at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:53) 
        at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106) 
        at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1013) 
        at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1072) 
        at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:809) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 
        at com.example.sheikhspc.testapp.Question4.onCreateView(Question4.java:36) 
        at android.app.Fragment.performCreateView(Fragment.java:2053) 
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894) 
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
        at android.app.BackStackRecord.run(BackStackRecord.java:834) 
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1454) 
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447) 
        at android.os.Handler.handleCallback(Handler.java:746) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5356) 
        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:905) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
I/Process: Sending signal. PID: 21504 SIG: 9 
Application terminated. 
+0

日志表明,一个OutOfMemoryError。你在片段中使用大的位图吗?如果是的话,那么一旦使用完成后,你必须回收位图变量,让gc()清除位图占用的内存。 – EEJ

+0

是的,我记得有一些问题。你能指导我一点,我在哪里做错了什么? –

+0

它显示错误在这一行的第二个片段 ** view1 = inflater.inflate(R.layout.fragment_question4,container,false); ** 我不知道我做错了什么。同一个项目运行良好。 –

回答

0

只是在AndroidManifest添加以下代码。 xml

android:largeHeap="true" 

AndroidManifest.xml中

<application 
android:allowBackup="true" 
android:icon="@mipmap/guruji" 
android:label="@string/app_name" 
android:supportsRtl="true" 
android:largeHeap="true" 
android:theme="@style/AppTheme">