Problem mit meinem Layout

  • 8 Antworten
  • Letztes Antwortdatum
Micka

Micka

Fortgeschrittenes Mitglied
1
Hy,

in meinem aktuellem Projekt habe ich Probleme mit meinem Layout.
Das Layout besteht lediglich aus TextViews und Buttons. Für die Anordnung habe ich ein TableLayout verwendet. Da unterhalb der TextViews 5 Buttons folgen habe ich mich dort für ein weiteres inneres Table Layout entschieden.

Zunächst mein Quellcode des Layouts(Ein Bild wie es aussieht hänge ich an):
Code:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Mainscreen" >

    <TableRow >
        <!-- <TextView
            android:id="@+id/txt_appname"
            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
            android:textSize="@string/text_normal_large" /> -->
    </TableRow>
    <TableRow
        android:layout_marginTop="25sp">
        <TextView
            android:id="@+id/txt_noten_beschriftung"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/text_noten"
            android:textSize="@string/text_normal_large" />
    </TableRow>
    <TableRow >
        <TextView
            android:id="@+id/txt_noten"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hopphopphopp"
            android:textSize="@string/text_normal_large" />
    </TableRow>
    <TableRow >
        <TableLayout >
            <TableRow 
                android:layout_marginTop="100sp">
                <Button
                    android:id="@+id/btn_note_g"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="G"
                    android:textSize="@string/text_normal_large" 
                    android:layout_marginRight="5sp" />
        
                <Button
                       android:id="@+id/btn_note_a"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@+id/txt_appname"
                    android:layout_alignTop="@+id/txt_noten_beschriftung"
                    android:text="A"
                    android:textSize="@string/text_normal_large" 
                    android:layout_marginLeft="5sp"
                    android:layout_marginRight="5sp" />
        
                <Button
                    android:id="@+id/btn_note_h"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_toLeftOf="@+id/txt_noten"
                    android:text="H"
                    android:textSize="@string/text_normal_large" 
                    android:layout_marginLeft="5sp"
                    android:layout_marginRight="5sp" />
        
                <Button
                    android:id="@+id/btn_note_c"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/txt_appname"
                    android:layout_alignBottom="@+id/txt_appname"
                    android:layout_centerHorizontal="true"
                    android:text="C"
                    android:textSize="@string/text_normal_large"
                    android:layout_marginLeft="5sp"
                    android:layout_marginRight="5sp" />

                <Button
                    android:id="@+id/btn_note_d"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/txt_noten"
                    android:layout_alignBottom="@+id/txt_noten"
                    android:layout_alignRight="@+id/txt_noten"
                    android:text="D"
                    android:textSize="@string/text_normal_large"
                    android:layout_marginLeft="5sp" />
            </TableRow>
        </TableLayout>
    </TableRow>
</TableLayout>
Ziel der App ist es angezeigte Musiknoten durch betätigen der jeweiligen Buttons nachzuspielen.

Quellcode der Activity:
Code:
package de.gerding.pentatonikpiano;

import android.media.AudioManager;
import android.media.SoundPool;
import android.media.SoundPool.OnLoadCompleteListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Toast;

public class Mainscreen extends Activity 
{
    private SoundPool mSoundPool;
    private int[] soundID = new int[5];
    private int[] sampleID = new int[5];
    private boolean[] loaded = new boolean[6];
    private int index = 0;
    private Button btn_note_g, btn_note_a, btn_note_h, btn_note_c, btn_note_d;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainscreen);
        btn_note_g = (Button) findViewById (R.id.btn_note_g);
        btn_note_a = (Button) findViewById (R.id.btn_note_a);
        btn_note_h = (Button) findViewById (R.id.btn_note_h);
        btn_note_c = (Button) findViewById (R.id.btn_note_c);
        btn_note_d = (Button) findViewById (R.id.btn_note_d);
        
        btn_note_g.setOnTouchListener(touchListener);
        btn_note_a.setOnTouchListener(touchListener);
        btn_note_h.setOnTouchListener(touchListener);
        btn_note_c.setOnTouchListener(touchListener);
        btn_note_d.setOnTouchListener(touchListener);
        
        //SoundPool-Stuff
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        
        mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() 
        {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId,int status) 
            {
                if(index < 5)
                {
                    loaded[index] = true;
                    sampleID[index] = sampleId;
                    index++;
                }
            }
        });
                
        for(int i=0;i<soundID.length;i++)
        {
            switch(i)
            {
                case 0: soundID[i] = mSoundPool.load(this, R.raw.g3, 1); break;
                case 1: soundID[i] = mSoundPool.load(this, R.raw.a4, 1); break;
                case 2: soundID[i] = mSoundPool.load(this, R.raw.b4, 1); break;
                case 3: soundID[i] = mSoundPool.load(this, R.raw.c4, 1); break;
                case 4: soundID[i] = mSoundPool.load(this, R.raw.d4, 1); break;
            }
        }
    }
    private boolean allLoaded()
    {
        boolean result = false;
        for(int i=0;i<4;i++)
        {
            if(loaded[i])
                result = true;
            else
                result = false;
        }
        return result;
    }
    private OnTouchListener touchListener = new OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) 
        {
            if(allLoaded())
            {
                if(event.getAction() == MotionEvent.ACTION_DOWN)
                {
                    AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
                    float actualVolume = (float) audioManager
                            .getStreamVolume(AudioManager.STREAM_MUSIC);
                    float maxVolume = (float) audioManager
                            .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
                    float volume = actualVolume / maxVolume;
                    
                    switch(v.getId())
                    {
                        case R.id.btn_note_g: sampleID[0] = mSoundPool.play(soundID[0], volume, volume, 1, -1, 1f); break;
                        case R.id.btn_note_a: sampleID[1] = mSoundPool.play(soundID[1], volume, volume, 1, 0, 1f); break;
                        case R.id.btn_note_h: sampleID[2] = mSoundPool.play(soundID[2], volume, volume, 1, 0, 1f); break;
                        case R.id.btn_note_c: sampleID[3] = mSoundPool.play(soundID[3], volume, volume, 1, 0, 1f); break;
                        case R.id.btn_note_d: sampleID[4] = mSoundPool.play(soundID[4], volume, volume, 1, 0, 1f); break;
                    }
                }
                else if(event.getAction() == MotionEvent.ACTION_UP)
                {
                    switch(v.getId())
                    {
                        case R.id.btn_note_g: mSoundPool.stop(sampleID[0]); break;
                        case R.id.btn_note_a: mSoundPool.stop(sampleID[1]); break;
                        case R.id.btn_note_h: mSoundPool.stop(sampleID[2]); break;
                        case R.id.btn_note_c: mSoundPool.stop(sampleID[3]); break;
                        case R.id.btn_note_d: mSoundPool.stop(sampleID[4]); break;
                    }
                }
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Tones not loaded!", Toast.LENGTH_SHORT).show();
            }
            
            
            return false;
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.mainscreen, menu);
        return true;
    }

}
Die App stürzt direkt beim Start ab. Scheinbar gibt es Probleme mit meinen TextViews, leider finde ich die Fehler aber nicht. Kann mir jemand sagen was ich falsch gemacht habe?

Logcat:
08-13 13:54:30.006: I/Process(12510): Sending signal. PID: 12510 SIG: 9
08-13 13:54:36.621: D/AndroidRuntime(12731): Shutting down VM
08-13 13:54:36.621: W/dalvikvm(12731): threadid=1: thread exiting with uncaught exception (group=0x41c332a0)
08-13 13:54:36.641: E/AndroidRuntime(12731): FATAL EXCEPTION: main
08-13 13:54:36.641: E/AndroidRuntime(12731): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.gerding.pentatonikpiano/de.gerding.pentatonikpiano.Mainscreen}: android.view.InflateException: Binary XML file line #12: Error inflating class <unknown>
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.app.ActivityThread.access$700(ActivityThread.java:140)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.os.Looper.loop(Looper.java:137)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.app.ActivityThread.main(ActivityThread.java:4921)
08-13 13:54:36.641: E/AndroidRuntime(12731): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 13:54:36.641: E/AndroidRuntime(12731): at java.lang.reflect.Method.invoke(Method.java:511)
08-13 13:54:36.641: E/AndroidRuntime(12731): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
08-13 13:54:36.641: E/AndroidRuntime(12731): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
08-13 13:54:36.641: E/AndroidRuntime(12731): at dalvik.system.NativeStart.main(Native Method)
08-13 13:54:36.641: E/AndroidRuntime(12731): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class <unknown>
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
08-13 13:54:36.641: E/AndroidRuntime(12731): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-13 13:54:36.641: E/AndroidRuntime(12731): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:313)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.app.Activity.setContentView(Activity.java:1924)
08-13 13:54:36.641: E/AndroidRuntime(12731): at de.gerding.pentatonikpiano.Mainscreen.onCreate(Mainscreen.java:28)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.app.Activity.performCreate(Activity.java:5206)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
08-13 13:54:36.641: E/AndroidRuntime(12731): ... 11 more
08-13 13:54:36.641: E/AndroidRuntime(12731): Caused by: java.lang.reflect.InvocationTargetException
08-13 13:54:36.641: E/AndroidRuntime(12731): at java.lang.reflect.Constructor.constructNative(Native Method)
08-13 13:54:36.641: E/AndroidRuntime(12731): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
08-13 13:54:36.641: E/AndroidRuntime(12731): ... 25 more
08-13 13:54:36.641: E/AndroidRuntime(12731): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x3
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.widget.TextView.<init>(TextView.java:930)
08-13 13:54:36.641: E/AndroidRuntime(12731): at android.widget.TextView.<init>(TextView.java:588)
08-13 13:54:36.641: E/AndroidRuntime(12731): ... 28 more
 
Hier noch das Bild wie es aussehen soll
 

Anhänge

  • PentatonikPiano - Layout.jpg
    PentatonikPiano - Layout.jpg
    26,1 KB · Aufrufe: 213
Hmm also der Infalter Fehler wirf bei Google immer was mit Bilder und Memory aus.
Setzt du irgendwo Bilder ein? Wenn ja, dann lösche die mal.. und versuch es ohne.

Gruß
 
Nö, ich verwende keine Bilder.

Gesendet von meinem GT-N8010 mit der Android-Hilfe.de App
 
Problem gelöst! Es lag daran das ich bei meinen Views die textgröße aus der strings.xml nehmen wollte. scheinbar klappt das nicht. habe mein Layout nun geändert, überall wo android:textSize="@string/text_large_large" oder ähnliches stand steht jetzt android:textSize="40sp". Siehe da die App läuft
 
TextSize ist ja auch kein String, sondern dimen!
 
ich dachte wenn der String 40sp ist es ja dasselbe als wenn ich 40sp manuell hinschreibe.

Gibt es ne Möglichkeit das irgendwie so zu realisieren?
 
Du kannst in die dimens 40sp rein schreiben. Halt so, wie du bei deinem TableLayout auch gemacht hast.

Also nur
<dimen name="text_large_large">40sp</dimen>

zur dimens.xml hinzufügen und statt android:textSize="@string/text_large_large" schreibst du

android:textSize="@dimen/text_large_large"
 
  • Danke
Reaktionen: Micka
Danke, mir war die dimen.xml bisher garnicht aufgefallen
 
Zurück
Oben Unten