Domanda

I have been working on creating a simple guitar tuner for my phone. I am fairly new to android development and have tried to understand the process of creating a guitar tuner with https://code.google.com/p/android-guitar-tuner/source/browse/#svn%2Ftrunk%2Fsrc%2Fcom%2Fexample%2FGuitarTuner which has helped immensely.

My problem has been getting the screen to show up instead of a black screen as the project is running. In the log I am printing out the best frequencies and they are printing out correctly. I have seen that using a custom View could help me, but it crashes every time I try to use one. I have a drawable image I'd like to use as the background and instead of using setContentView(R.layout.activity_tuner) I'd like to find a way to use my custom view Analyze to do something like setContentView(new Analyze(this)).

Heres the main activity, xml, and custom view class I'm working with, I'm mainly asking for guidance on where to start to solve my problem or help on resources on how I can set up a custom view using a drawable image if thats possible.

I also understand there is nothing in my Custom view class, I have tried a few things using Bitmaps, but to no avail. I erased the code that was there and replaced setContentView(new Analyze(this)) with setContentView(R.layout.activity_tuner) so I could test the frequencies with a guitar in the logcat.

activity_tuner.xml:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.example.thenxttuner"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/graybackgroundtunerpage"
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=".Tuner" >
<com.example.thenxttuner.Analyze
    android:id="@+id/custView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    />
<ImageView
    android:id="@+id/display"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/dashdrawable" />

<Button
    android:id="@+id/start"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/display"
    android:layout_below="@+id/display"
    android:layout_marginTop="21dp"
    android:background="@color/limegreen"
    android:text="Stop Tuning" />

</RelativeLayout> 

Tuner.java:

public class Tuner extends Activity{
boolean recording = true;
Button start;
Thread myThread;
TextView text, texttwo, textthree;
private double pitch;
Analyze back;
private static final String TAG = "Tuner";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tuner);
    start = (Button) findViewById(R.id.start);

    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(recording==true){
                start.setText("Stop Tuning");
                recording=false;
                onStart();
            }else{
                start.setText("Start Tuning");
                recording=true;
                onStop();
            }
        }
    });
}

@Override
public void onStart(){
    super.onStart();
    //setContentView(R.layout.activity_tuner);
    //Log.v(TAG,  "in onStart()");

    myThread = new Thread(new Detector(this, new Handler()));
    myThread.run();
}

@Override
public void onStop(){
    super.onStop();
    //Log.v(TAG, "in onStop()");

    myThread.interrupt();
}


public void ShowResult(final HashMap<Double, Double> frequencies, final double best){
    Log.v(TAG,  "best = "+best);

    pitches[0][0] = 82.41;
    pitches[0][1] = 87.31;
    pitches[0][2] = 92.5;
    pitches[0][3] = 98;
    pitches[0][4] = 103.8;
    pitches[1][0] = 110;
    pitches[1][1] = 116.54;
    pitches[1][2] = 123.48;
    pitches[1][3] = 130.82;
    pitches[1][4] = 138.59;
    pitches[2][0] = 147.83;
    pitches[2][1] = 155.56;
    pitches[2][2] = 164.81;
    pitches[2][3] = 174.62;
    pitches[2][4] = 185;
    pitches[3][0] = 196;
    pitches[3][1] = 207;
    pitches[3][2] = 220;
    pitches[3][3] = 233.08;
    pitches[4][0] = 246.96;
    pitches[4][1] = 261.63;
    pitches[4][2] = 277.18;
    pitches[4][3] = 293.66;
    pitches[4][4] = 311.13;
    pitches[5][0] = 329.63;
    pitches[5][1] = 349.23;
    pitches[5][2] = 369.99;
    pitches[5][3] = 392;
    pitches[5][4] = 415.3;
    pitches[5][5] = 440;
}
double[][] pitches = new double[6][6];
}

Analyze.java:

public class Analyze extends View{
private static final String TAG = "Analyze";
private final static int UI_UPDATE = 100;
private Handler AnaHandler;
private Timer timer;

public Analyze(Context context) {
    // TODO Auto-generated constructor stub
    super(context);



    /**UI update
    AnaHandler = new Handler();
    timer = new Timer();

    timer.schedule(new TimerTask(){
        public void run(){
            AnaHandler.post(new Runnable(){
                public void run(){
                    invalidate();
                }
            });
        }
    }, 
    UI_UPDATE,
    UI_UPDATE);**/ 
    // not sure if needed 


}

@Override
public void onDraw(Canvas canvas){

}



}
È stato utile?

Soluzione

Your app us running in Uİ Thread, ur calling the run method of the thread... u have to use start-method, otherwise it is just a normal method call.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top