1 Error needs fix - cannot make a static reference to the non static method

  • Replies:0
John A B Smith
  • Forum posts: 1

Apr 22, 2014, 1:55:02 PM via Website

Hello,

This is my first post and first venture onto a forum about Android Development.

I've been working on this small android app for the last week and half and have done about as much as possible without anyone's help so far via online tutorials.

I have a specific issue now which I have not been able to resolve and I've been reading and trying different things to get it working since yesterday.

I have a main activity called GuitarActivity java and its layout activity_guitar xml.

There is a row of buttons which pull in a fragment called guitar_tune_01 xml

The fragment has a row of buttons which need to play a different sound each.

I have implemented the fragment code into the main activity GuitarActivity java

I am getting one last error which I really need someone to help me fix.

Error is on line 38 and all recurring instances: MediaPlayer mp = MediaPlayer.create(getApplicationContext(),

Error says

rename in file (ctrl+2, r)

and

cannot make a static reference to the non static method getapplicationcontext from the type contextwrapper

MyFragment1 is a static class, and I can't reference the non-static method from there. I need to move the fragment to the outer scope (its own class file) and remove the static modifier on the class declaration, and that should do it.

This was from someone who was trying to help me but is over my head.

Please help.

Many thanks, J

GuitarActivity java

package com.example.androidapptest;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;

public class GuitarActivity extends FragmentActivity {

    public static class MyFragment1 extends Fragment {

        TextView textMsg;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_guitar_tune_01, null);
            textMsg = (TextView)view.findViewById(R.id.textmsg);

            Button button7 = (Button) view.findViewById(R.id.button_guitar_note1);
            Button button8 = (Button) view.findViewById(R.id.button_guitar_note2);
            Button button9 = (Button) view.findViewById(R.id.button_guitar_note3);

            button7.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // Play button sound
                    MediaPlayer mp = MediaPlayer.create(getApplicationContext(),
                            R.raw.guitar_e_standard_1_e);
                    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            mediaPlayer.start();
                        }
                    });

                }
            });

            button8.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // Play button sound
                    MediaPlayer mp = MediaPlayer.create(getApplicationContext(),
                            R.raw.guitar_e_standard_2_a);
                    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            mediaPlayer.start();
                        }
                    });

                }
            });

            button9.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // Play button sound
                    MediaPlayer mp = MediaPlayer.create(getApplicationContext(),
                            R.raw.guitar_e_standard_3_d);
                    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            mediaPlayer.start();
                        }
                    });

                }
            });

            Bundle bundle = getArguments();
            if(bundle != null){
                String msg = bundle.getString(KEY_MSG_1);
                if(msg != null){
                    textMsg.setText(msg);
                }
            }

            return view;
        }

        public void setMsg(String msg){
            textMsg.setText(msg);
        }

    }

    public static class MyFragment2 extends Fragment {

        TextView textMsg;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_guitar_tune_02, null);
            textMsg = (TextView)view.findViewById(R.id.textmsg);

            Bundle bundle = getArguments();
            if(bundle != null){
                String msg = bundle.getString(KEY_MSG_2);
                if(msg != null){
                    textMsg.setText(msg);
                }
            }

            return view;
        }

        public void setMsg(String msg){
            textMsg.setText(msg);
        }

    }

    public static class MyFragment3 extends Fragment {

        TextView textMsg;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_guitar_tune_03, null);
            textMsg = (TextView)view.findViewById(R.id.textmsg);

            Bundle bundle = getArguments();
            if(bundle != null){
                String msg = bundle.getString(KEY_MSG_3);
                if(msg != null){
                    textMsg.setText(msg);
                }
            }

            return view;
        }

        public void setMsg(String msg){
            textMsg.setText(msg);
        }

    }

    FrameLayout container;
    FragmentManager myFragmentManager;
    MyFragment1 myFragment1;
    MyFragment2 myFragment2;
    MyFragment3 myFragment3;
    final static String TAG_1 = "FRAGMENT_1";
    final static String TAG_2 = "FRAGMENT_2";
    final static String TAG_3 = "FRAGMENT_3";
    final static String KEY_MSG_1 = "FRAGMENT1_MSG";
    final static String KEY_MSG_2 = "FRAGMENT2_MSG";
    final static String KEY_MSG_3 = "FRAGMENT3_MSG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guitar);

        // Define home buttons
        Button button0 = (Button) findViewById(R.id.home_page_go);

        // Fragment container
        container = (FrameLayout)findViewById(R.id.maincontainer);

        // Buttons to change the fragment
        // Spinner menu should be used instead
        Button button1 = (Button)findViewById(R.id.button1);
        Button button2 = (Button)findViewById(R.id.button2);
        Button button3 = (Button)findViewById(R.id.button3);

        // Home button sound
        button0.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // Code to execute
                Intent myintent2 = new Intent(GuitarActivity.this,HomeActivity.class);
                startActivity(myintent2);

                // Play button sound
                MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.button);
                mp.start();

            }
        });

        // Fragment stuff

        button1.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {

                MyFragment1 fragment = (MyFragment1)myFragmentManager.findFragmentByTag(TAG_1);

                if (fragment == null) {

                    Bundle bundle = new Bundle();
                    bundle.putString(KEY_MSG_1, "Replace MyFragment1");
                    myFragment1.setArguments(bundle);

                    FragmentTransaction fragmentTransaction = myFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.maincontainer, myFragment1, TAG_1);
                    fragmentTransaction.commit();

                }else{

                    fragment.setMsg("MyFragment1 already loaded");
                }
            }});

        button2.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {

                MyFragment2 fragment = (MyFragment2)myFragmentManager.findFragmentByTag(TAG_2);

                if (fragment == null) {

                    Bundle bundle = new Bundle();
                    bundle.putString(KEY_MSG_2, "Replace MyFragment2");
                    myFragment2.setArguments(bundle);

                    FragmentTransaction fragmentTransaction = myFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.maincontainer, myFragment2, TAG_2);
                    fragmentTransaction.commit();

                }else{
                    fragment.setMsg("MyFragment2 already loaded");
                }
            }});

        button3.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {

                MyFragment3 fragment = (MyFragment3)myFragmentManager.findFragmentByTag(TAG_3);

                if (fragment == null) {

                    Bundle bundle = new Bundle();
                    bundle.putString(KEY_MSG_3, "Replace MyFragment3");
                    myFragment3.setArguments(bundle);

                    FragmentTransaction fragmentTransaction = myFragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.maincontainer, myFragment3, TAG_3);
                    fragmentTransaction.commit();

                }else{
                    fragment.setMsg("MyFragment3 already loaded");
                }
            }});

        myFragmentManager = getSupportFragmentManager();
        myFragment1 = new MyFragment1();
        myFragment2 = new MyFragment2();
        myFragment3 = new MyFragment3();

        if(savedInstanceState == null){
            //if's the first time created

            FragmentTransaction fragmentTransaction = myFragmentManager.beginTransaction();
            fragmentTransaction.add(R.id.maincontainer, myFragment1, TAG_1);
            fragmentTransaction.commit();
        }

    }

}

activity_guitar xml

<LinearLayout xmlns:android="forum doesn't like this bit"
    xmlns:tools="or this but"
    tools:context="com.example.androidapptest.GuitarActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/home_page_go"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="GuitarActivity" />

        <View
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_weight="1" >
        </View>

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            style="@style/button_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />

        <Button
            android:id="@+id/button2"
            style="@style/button_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />

        <Button
            android:id="@+id/button3"
            style="@style/button_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/maincontainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

</LinearLayout>

fragment_guitar_tune_01 xml

<LinearLayout xmlns:android="forum doesn't like this bit"
    xmlns:tools="or this bit"
    tools:context="com.example.androidapptest.GuitarActivity"
    android:id="@+id/container1"
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="0.22"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textmsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/guitar_tune_frag_01"
        android:visibility="gone" />

    <Button
        android:id="@+id/button_guitar_note1"
        style="@style/button_text"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="bottom"
        android:text="@string/guitar_note_01" />

    <Button
        android:id="@+id/button_guitar_note2"
        style="@style/button_text"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="bottom"
        android:text="@string/guitar_note_02" />

    <Button
        android:id="@+id/button_guitar_note3"
        style="@style/button_text"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="bottom"
        android:text="@string/guitar_note_03" />

</LinearLayout>

— modified on Apr 22, 2014, 2:34:50 PM

Reply