SurfaceView in ViewPager stürzt ab

  • 2 Antworten
  • Letztes Antwortdatum
A

astro-android

Neues Mitglied
2
Hallo,

da ich den ganzen Tag gegoogelt habe und ich keine Idee habe, was ich falsch gemacht habe, möchte ich mich mit meinem (für die meisten von Euch wahrscheinlich banalen) Problem an dieses Forum wenden. Es geht ganz einfach darum, eine von SurfaceView abgeleitete, eigene View in einem Viewpager unterzubringen, ohne dass die App abstürzt. Hier mein Code:

Erstmal das Layout der buggy Seite::thumbdn:

Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ddd" 
    android:orientation="vertical" 
    android:padding="@dimen/padding_layout"> 

    <de.test.check.Graphic
        android:id="@+id/page2_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:layout_above="@+id/textView1" 
        android:layout_alignParentTop="true" 
        android:background="#888"/>
    
    <TextView 
        android:id="@+id/textView1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentRight="true"/>

    <Button 
        android:id="@+id/button4" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:layout_alignRight="@+id/textView1" 
        android:layout_below="@+id/page2_view" 
        android:background="#ddd" 
        android:text="@string/button_next" 
        android:textColor="@color/col_back_view_1" 
        android:textSize="@dimen/preamble_text_size"/>
    
    <Button 
        android:id="@+id/button3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignLeft="@+id/textView1" 
        android:layout_alignTop="@+id/textView1" 
        android:background="#ddd" 
        android:text="@string/button_back" 
        android:textColor="@color/col_back_view_1" 
        android:textSize="@dimen/preamble_text_size"/>
    
</RelativeLayout>
Die Java-View::thumbdn:

Code:
package de.test.check;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class Graphic extends SurfaceView implements Callback {

    public static SurfaceView graphicsView;
    private SurfaceHolder holder;
    private Canvas canvas;
    private Paint paint;
    private Thread th;
    private boolean isRunning = false;

    public static void putGraphic(){
    }        

    public Graphic(Context context) {
        super(context);
        init();        
    }
    
    public void init(){
        
        holder = this.getHolder();
        holder.addCallback(this);

        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        
        //Thread zum Zeichnen
        new Thread(new Runnable() { 
            public void run(){                 
                while(isRunning)
                {
                    draw(canvas);
                    try{
                        Thread.sleep(50);
                    } catch (InterruptedException e){}
                }         
            }
        }).start();
        
        isRunning = true;
    }
    
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    public void surfaceCreated(SurfaceHolder holder) {
    }
    
    public void surfaceDestroyed(SurfaceHolder holder) {
    }
    
    public void draw(Canvas c) {     
        c = holder.lockCanvas();
        if(c != null) {
                   
            c.drawColor(Color.LTGRAY);
            c.drawCircle(400, 450, 350, paint);                 
                    
            holder.unlockCanvasAndPost(c);
        }
    }
}
Und die Activity::thumbdn:

Code:
package de.test.check;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


public class Check extends Activity implements TextToSpeech.OnInitListener{

    public static Locale  loc;
    public static String sloganString;
    public static int counter = 0;
    public static TextToSpeech tts;
    public static SurfaceView graphicView;
    
    @TargetApi(14)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        Config.context = this;

        loc = Locale.getDefault();

        // Initialize Text To speech
        tts = new TextToSpeech(Config.context, Config.context);
        
        sloganString = Config.context.getResources().getString(R.string.start_slogan);

        Graphic view = new Graphic(Config.context);

        List<View> viewPages = new ArrayList<View>();
        ViewPager viewPager = new ViewPager(Config.context);
        setContentView(view);

        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

        View viewPage = inflater.inflate(R.layout.page_1, null);

        TextView preambleView = (TextView) viewPage.findViewById(R.id.page1_view);
        CharSequence styledText = Html.fromHtml(getResources().getString(R.string.preamble));        
        preambleView.setText(styledText);
        viewPages.add(viewPage);
        
        viewPage = inflater.inflate(R.layout.page_2, null);

        graphicView = (SurfaceView) viewPage.findViewById(R.id.page2_view);
        viewPages.add(viewPage);
        
        viewPage = inflater.inflate(R.layout.page_3, null);

        ImageView legendView = (ImageView)  viewPage.findViewById(R.id.page3_view);
        viewPages.add(viewPage);
        
        PageAdapter pageAdapter = new PageAdapter(viewPages);
        viewPager.setAdapter(pageAdapter);
        viewPager.setCurrentItem(1);     

        //DoIt.all();

        //UpdateNow.update.sendEmptyMessage(0); /// Starten
      }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public void onInit(int success) {
        if(success == TextToSpeech.SUCCESS){
            if(tts.isLanguageAvailable(loc) >= TextToSpeech.LANG_AVAILABLE){
                tts.setLanguage(loc);
                   SpeakIt.now(CheckNow.sloganString);
            }
         }
        else{
            tts.shutdown();
            tts = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (tts != null) {
            tts.stop();
            tts.shutdown();
            tts = null;
        }
    }
}
Also eigentlich ganz primiv, stürzt aber trotzdem ab! Irgendwas muss ich wohl nicht richtig gemacht haben. Bin ja auch noch ganz am Anfang mit Android...:blushing: Vielleicht hat ja jemand Mitleid und sagt mir, was ich falsch mache !? Die View alleine funzt...

9FT09xVD3lNbF7TMFbDHvaxkG1CEAAAOw==
 
Zuletzt bearbeitet:
Was sagt der Logcat? Das ist die erste Anlaufstelle beim beheben eines Fehlers.
 
Der sagt, daß es Probleme mit dem InflateView gibt ... :huh:

Code:
11-01 19:08:58.104: E/AndroidRuntime(3408): FATAL EXCEPTION: main
11-01 19:08:58.104: E/AndroidRuntime(3408): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.test.check/de.test.check.Check}: android.view.InflateException: Binary XML file line #10: Error inflating class de.test.check.Graphic
11-01 19:08:58.104: E/AndroidRuntime(3408):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
 
Zuletzt bearbeitet:
Zurück
Oben Unten