title: 1 Error needs fix - cannot make a static reference to the non static method
body: <p>This is my first post and first venture onto a forum about Android Development.</p>
  
  <p>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.</p>
  
  <p>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 9 this morning, including searching and trying various answers to similar questions right here.</p>
  
  <p>I have a main activity which has a row of buttons to bring in different fragments.</p>
  
  <p>The fragments have a row of buttons which need to play a different sound each.</p>
  
  <p>I have created a separate app to test that the fragment buttons and java is working but this Java does not work when used with my fragment in the main app. I really need someone to just show me exactly what I need to add to get it working.</p>
  
  <p>I understand that there is a lot of inefficiency with everything else but I would just like to concentrate on this one problem first. I just want to get it working and then with time I can change code as I develop a better understanding of what is going on as I learn Java.</p>
  
  <p>The fragments swap is working correctly. GuitarActivity.java/activity_guitar.xml correctly pulls in fragment_guitar_tune_01.xml. I just need the java for GuitarTune01Activity.java to now work with fragment_guitar_tune_01.xml</p>
  
  <p>Please help.</p>
  
  <p>Many thanks, Jo</p>
  
  <p><strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.androidapptest"
  android:versionCode="1"
  android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="19" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.androidapptest.HomeActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
          &lt;activity
              android:name="com.example.androidapptest.GuitarActivity"
              android:label="@string/title_activity_guitar" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name="com.example.androidapptest.BassActivity"
              android:label="@string/title_activity_bass" &gt;
          &lt;/activity&gt;
          &lt;activity
              android:name="com.example.androidapptest.ChordsActivity"
              android:label="@string/title_activity_chords" &gt;
          &lt;/activity&gt;
          &lt;activity android:name="com.example.androidapptest.GuitarTune01Activity" &gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>activity_guitar.xml</strong></p>
  
  <p>Row of buttons to change the fragment in framelayout.</p>
  
  <pre><code>&lt;LinearLayout 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:background="@drawable/bg_guitar_page"
      android:orientation="vertical"
      tools:context="com.example.androidapptest.GuitarActivity" &gt;
  
      &lt;LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal" &gt;
  
          &lt;Button
              android:id="@+id/home_page_go"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:background="@drawable/button_home_define"
              android:onClick="GuitarActivity" /&gt;
  
          &lt;View
              android:layout_width="0dp"
              android:layout_height="1dp"
              android:layout_weight="1" &gt;
          &lt;/View&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal" &gt;
  
          &lt;Button
              android:id="@+id/button1"
              style="@style/button_text"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:background="@drawable/button_general_define"
              android:text="1" /&gt;
  
          &lt;Button
              android:id="@+id/button2"
              style="@style/button_text"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:background="@drawable/button_general_define"
              android:text="2" /&gt;
  
          &lt;Button
              android:id="@+id/button3"
              style="@style/button_text"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:background="@drawable/button_general_define"
              android:text="3" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;FrameLayout
          android:id="@+id/maincontainer"
          android:layout_width="match_parent"
          android:layout_height="match_parent" &gt;
      &lt;/FrameLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>GuitarActivity.java</strong></p>
  
  <p>This contains the code to pull in fragments by clicking the buttons in the above xml.</p>
  
  <pre><code>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);
  
              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();
          }
  
          }
  
      }
  </code></pre>
  
  <p><strong>fragment_guitar_tune_01.xml</strong></p>
  
  <p>This is the fragment with a row of buttons to play sounds.</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      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"
      android:paddingBottom="30dp"
      tools:context="com.example.androidapptest.GuitarTune01Activity" &gt;
  
      &lt;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" /&gt;
  
      &lt;Button
          android:id="@+id/button_guitar_note1"
          style="@style/button_text"
          android:layout_width="40dp"
          android:layout_height="40dp"
          android:layout_gravity="bottom"
          android:background="@drawable/button_general_define"
          android:text="@string/guitar_note_01" /&gt;
  
      &lt;Button
          android:id="@+id/button_guitar_note2"
          style="@style/button_text"
          android:layout_width="40dp"
          android:layout_height="40dp"
          android:layout_gravity="bottom"
          android:background="@drawable/button_general_define"
          android:text="@string/guitar_note_02" /&gt;
  
      &lt;Button
          android:id="@+id/button_guitar_note3"
          style="@style/button_text"
          android:layout_width="40dp"
          android:layout_height="40dp"
          android:layout_gravity="bottom"
          android:background="@drawable/button_general_define"
          android:text="@string/guitar_note_03" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>GuitarTune01Activity.java</strong></p>
  
  <p>This java if for the fragment. It is to play a different sound for a row of buttons located in the fragment. This code does not work with the fragment but works when tested seperately.</p>
  
  <pre><code>package com.example.androidapptest;
  
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v7.app.ActionBarActivity;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.content.Context;
  import android.media.MediaPlayer;
  import android.os.Bundle;
  
  public class GuitarTune01Activity extends ActionBarActivity {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.fragment_guitar_tune_01);
  
          Button button7 = (Button) findViewById(R.id.button_guitar_note1);
          Button button8 = (Button) findViewById(R.id.button_guitar_note2);
          Button button9 = (Button) 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();
                      }
                  });
  
              }
          });
  
      }
  
  }
  </code></pre>
  
  <p><strong>CHANGES</strong></p>
  
  <p>I have made the change suggested and after correcting a few errors (putting view.findViewbyID) I only have one error left to correct before I can test it.</p>
  
  <p>Error is on line: <em>MediaPlayer mp = MediaPlayer.create(getApplicationContext(),</em></p>
  
  <p>It says</p>
  
  <p><em>rename in file (ctrl+2, r)</em></p>
  
  <p>and</p>
  
  <p><em>cannot make a static reference to the non static method getapplicationcontext from the type contextwrapper</em></p>
  
  <p>Any suggestions? Thanks</p>
  
  <pre><code>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);
          }
  
      }
  </code></pre>