Eine von SurfaceView abgeleitete Java-View stürzt in einem ViewPager ab

  • Antworten:0
Manfred K.
  • Forum-Beiträge: 11

02.11.2012, 13:27:17 via Website

Hallo, liebes Forum,

Bin mit der Programmierung von Android noch nicht lange zugange, um es mal vorweg zu nehmen.
Programmieren kann ich aber schon lange. Jedenfalls im Moment bin ich ziemlich gefrustet, weil meine App appstürzt. Ich habe jetzt 2 Tage lang versucht, den Fehler zu finden und habe dafür das Internet auf links gedreht.
Ich hoffe, dass jemand mir hier auf die Sprünge helfen kann. Die Sache ist eigentlich ziemlich simpel:

In einem einfachen ViewPager gibt es 3 Seiten. Eine davon ist eine eigene View,
in der ich eine Grafik zyklisch aktualisieren will. Die App wird zwar ohne Probleme compiliert, stürzt dann aber auf meinem Nexus7 ab (auch auf einem Motorola Pro+ - Smartphone).

Hier der Code:

Activity:

1package de.test.check;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.Locale;
6
7import android.annotation.TargetApi;
8import android.app.Activity;
9import android.os.Bundle;
10import android.speech.tts.TextToSpeech;
11import android.support.v4.view.ViewPager;
12import android.text.Html;
13import android.view.LayoutInflater;
14import android.view.Menu;
15import android.view.SurfaceView;
16import android.view.View;
17import android.widget.ImageView;
18import android.widget.TextView;
19
20
21public class Check extends Activity implements TextToSpeech.OnInitListener{
22
23 public static Locale loc;
24 public static String sloganString;
25 public static int counter = 0;
26 public static TextToSpeech tts;
27 public static Graphic graphicView;
28
29 @TargetApi(14)
30 @Override
31 public void onCreate(Bundle savedInstanceState) {
32 super.onCreate(savedInstanceState);
33
34 Config.context = this;
35
36 loc = Locale.getDefault();
37
38 // Initialize Text To speech
39 tts = new TextToSpeech(Config.context, Config.context);
40
41 sloganString = Config.context.getResources().getString(R.string.start_slogan);
42
43 Graphic view = new Graphic(Config.context);
44
45 List<View> viewPages = new ArrayList<View>();
46 ViewPager viewPager = new ViewPager(Config.context);
47 setContentView(view);
48
49 LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
50
51 View viewPage = inflater.inflate(R.layout.page_1, null);
52
53 TextView preambleView = (TextView) viewPage.findViewById(R.id.page1_view);
54 CharSequence styledText = Html.fromHtml(getResources().getString(R.string.preamble));
55 preambleView.setText(styledText);
56 viewPages.add(viewPage);
57
58 viewPage = inflater.inflate(R.layout.page_2, null);
59
60 graphicView = (Graphic) viewPage.findViewById(R.id.page2_view);
61 viewPages.add(viewPage);
62
63 viewPage = inflater.inflate(R.layout.page_3, null);
64
65 ImageView legendView = (ImageView) viewPage.findViewById(R.id.page3_view);
66 viewPages.add(viewPage);
67
68 PageAdapter pageAdapter = new PageAdapter(viewPages);
69 viewPager.setAdapter(pageAdapter);
70 viewPager.setCurrentItem(1);
71
72 //DoIt.all();
73
74 //UpdateNow.update.sendEmptyMessage(0); /// Starten
75 }
76
77 @Override
78 public boolean onCreateOptionsMenu(Menu menu) {
79 getMenuInflater().inflate(R.menu.menu, menu);
80 return true;
81 }
82
83 @Override
84 public void onInit(int success) {
85 if(success == TextToSpeech.SUCCESS){
86 if(tts.isLanguageAvailable(loc) >= TextToSpeech.LANG_AVAILABLE){
87 tts.setLanguage(loc);
88 SpeakIt.now(Check.sloganString);
89 }
90 }
91 else{
92 tts.shutdown();
93 tts = null;
94 }
95 }
96
97 @Override
98 protected void onDestroy() {
99 super.onDestroy();
100 if (tts != null) {
101 tts.stop();
102 tts.shutdown();
103 tts = null;
104 }
105 }
106}

der Page-Adapter:

1package de.test.check;
2
3import java.util.List;
4
5import android.os.Parcelable;
6import android.support.v4.view.ViewPager;
7import android.support.v4.view.PagerAdapter;
8import android.view.View;
9
10public class PageAdapter extends PagerAdapter{
11
12 List<View> pages = null;
13
14 public PageAdapter(List<View> pages){
15 this.pages = pages;
16 }
17
18 @Override
19 public Object instantiateItem(View collection, int position){
20 View v = pages.get(position);
21 ((ViewPager) collection).addView(v, 0);
22 return v;
23 }
24
25 @Override
26 public void destroyItem(View collection, int position, Object view){
27 ((ViewPager) collection).removeView((View) view);
28 }
29
30 @Override
31 public int getCount(){
32 return pages.size();
33 }
34
35 @Override
36 public boolean isViewFromObject(View view, Object object){
37 return view.equals(object);
38 }
39
40 @Override
41 public void finishUpdate(View arg0){
42 }
43
44 @Override
45 public void restoreState(Parcelable arg0, ClassLoader arg1){
46 }
47
48 @Override
49 public Parcelable saveState(){
50 return null;
51 }
52
53 @Override
54 public void startUpdate(View arg0){
55 }
56}

die Java-View:

1package de.test.check;
2
3import android.content.Context;
4import android.graphics.Canvas;
5import android.graphics.Color;
6import android.graphics.Paint;
7import android.view.SurfaceHolder;
8import android.view.SurfaceHolder.Callback;
9import android.view.SurfaceView;
10
11public class Graphic extends SurfaceView implements Callback {
12
13 public static SurfaceView graphicsView;
14 private SurfaceHolder holder;
15 private Canvas canvas;
16 private Paint paint;
17 private Thread th;
18 private boolean isRunning = false;
19
20 public static void putGraphic(){
21 }
22
23 public Graphic(Context context) {
24 super(context);
25 init();
26 }
27
28 public void init(){
29
30 holder = this.getHolder();
31 holder.addCallback(this);
32
33 paint = new Paint();
34 paint.setColor(Color.RED);
35 paint.setStyle(Paint.Style.STROKE);
36
37 //Thread zum Zeichnen
38 new Thread(new Runnable() {
39 public void run(){
40 while(isRunning)
41 {
42 draw(canvas);
43 try{
44 Thread.sleep(50);
45 } catch (InterruptedException e){}
46 }
47 }
48 }).start();
49
50 isRunning = true;
51 }
52
53 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
54 }
55
56 public void surfaceCreated(SurfaceHolder holder) {
57 }
58
59 public void surfaceDestroyed(SurfaceHolder holder) {
60 }
61
62 public void draw(Canvas c) {
63 c = holder.lockCanvas();
64 if(c != null) {
65
66 c.drawColor(Color.LTGRAY);
67 c.drawCircle(400, 450, 350, paint);
68
69 holder.unlockCanvasAndPost(c);
70 }
71 }
72}

und das XML-Layout:

1<?xml version="1.0" encoding="utf-8"?>
2<RelativeLayout
3 xmlns:android="... Link darf hier nicht"/schemas.android.com/apk/res/android"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:background="#ddd"
7 android:orientation="vertical"
8 android:padding="@dimen/padding_layout">
9
10 <de.astropartie.check.Graphic << Fehler !
11 android:id="@+id/page2view"
12 android:layout_width="fill_parent"
13 android:layout_height="wrap_content"
14 android:layout_above="@+id/textView1"
15 android:layout_alignParentTop="true"
16 android:background="#888"/>
17
18 <TextView
19 android:id="@+id/textView1"
20 android:layout_width="match_parent"
21 android:layout_height="wrap_content"
22 android:layout_alignParentBottom="true"
23 android:layout_alignParentLeft="true"
24 android:layout_alignParentRight="true"/>
25
26 <Button
27 android:id="@+id/button4"
28 android:layout_width="wrap_content"
29 android:layout_height="wrap_content"
30 android:layout_alignParentBottom="true"
31 android:layout_alignRight="@+id/textView1"
32 android:layout_below="@+id/page2_view"
33 android:background="#ddd"
34 android:text="@string/button_next"
35 android:textColor="@color/col_back_view_1"
36 android:textSize="@dimen/preamble_text_size"/>
37
38 <Button
39 android:id="@+id/button3"
40 android:layout_width="wrap_content"
41 android:layout_height="wrap_content"
42 android:layout_alignLeft="@+id/textView1"
43 android:layout_alignTop="@+id/textView1"
44 android:background="#ddd"
45 android:text="@string/button_back"
46 android:textColor="@color/col_back_view_1"
47 android:textSize="@dimen/preamble_text_size"/>
48
49</RelativeLayout>

das LogCat hierzu:

111-02 12:10:19.196: I/TextToSpeech(22877): Sucessfully bound to com.google.android.tts
2
311-02 12:10:19.346: D/AndroidRuntime(22877): Shutting down VM
4
511-02 12:10:19.346: W/dalvikvm(22877): threadid=1: thread exiting with uncaught exception (group=0x40c9b300)
6
711-02 12:10:19.396: E/AndroidRuntime(22877): FATAL EXCEPTION: main
8
911-02 12:10:19.396: E/AndroidRuntime(22877): java.lang.RuntimeException: Unable to start activity
10 ComponentInfo{de.test.check/de.test.check.Check}: android.view.InflateException:
11 Binary XML file line #10: Error inflating class de.test.check.Graphic

Ich wäre euch unendlich dankbar, wenn mir hier jemand helfen würde !!!

Danke

Antworten