2016-10-02 1 views
0

Ich bin neu in Android. Ich bewege zwei Fragmente über einen Countdown-Timer. nach 10 Sekunden sollte das nächste Fragment angezeigt werden, aber meine App funktioniert nicht mehr. Ich habe versucht, eine Lösung, aber konnte nicht dafür geeignet finden. Bitte helfen Sie mir.App stoppt mit Fragmenten mit Countdown-Timer

Question3.java (Erste Fragment)

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 (Zweite Fragment)

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) { 
} 
} 

Error Log

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

Das Protokoll zeigt, dass es eine OutOfMemoryError. Verwenden Sie große Bitmaps in Fragment. Wenn ja, müssen Sie die Bitmap-Variablen nach Beendigung der Verwendung wiederverwenden, damit gc() den von der Bitmap belegten Speicher bereinigt. – EEJ

+0

Ja, ich habe ein Problem mit dem Speicher. Kannst du mir bitte ein bisschen zeigen, wo ich etwas falsch mache? –

+0

es zeigt Fehler in dieser Zeile des zweiten Fragments ** view1 = inflater.inflate (R.layout.fragment_question4, Container, falsch); ** Ich nicht, was ich falsch gemacht habe. Das gleiche Projekt lief gut. –

Antwort

0

einfach den folgenden Code in der AndroidManifest hinzuzufügen. 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">