1错误需要修复 - 无法对非静态方法进行静态引用

时间:2014-04-21 17:03:06

标签: android

这是我的第一篇文章,也是第一篇关于Android开发的论坛。

我上周和上半年一直在研究这款小型Android应用程序,并且通过在线教程,在没有任何人帮助的情况下做了尽可能多的工作。

我现在有一个特定的问题,我无法解决这个问题,我一直在阅读并尝试不同的事情,以便在今天上午9点开始工作,包括在这里搜索并尝试各种类似问题的答案。

我有一个主要的活动,它有一排按钮来引入不同的片段。

片段有一排按钮,每个按钮需要播放不同的声音。

我创建了一个单独的应用程序来测试片段按钮和java是否正常工作,但是当与主应用程序中的片段一起使用时,这个Java不起作用。我真的需要有人向我展示我需要添加的内容以使其正常工作。

我知道其他一切都有很多低效率,但我只想先关注这个问题。我只是想让它工作,然后随着时间的推移,我可以更改代码,因为我在学习Java的过程中更好地理解了正在发生的事情。

片段交换正常工作。 GuitarActivity.java/activity_guitar.xml正确地插入fragment_guitar_tune_01.xml。我只需要用于GuitarTune01Activity.java的java即可使用fragment_guitar_tune_01.xml

请帮忙。

非常感谢,乔

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidapptest"
android:versionCode="1"
android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.androidapptest.HomeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.androidapptest.GuitarActivity"
            android:label="@string/title_activity_guitar" >
        </activity>
        <activity
            android:name="com.example.androidapptest.BassActivity"
            android:label="@string/title_activity_bass" >
        </activity>
        <activity
            android:name="com.example.androidapptest.ChordsActivity"
            android:label="@string/title_activity_chords" >
        </activity>
        <activity android:name="com.example.androidapptest.GuitarTune01Activity" >
        </activity>
    </application>

</manifest>

activity_guitar.xml

用于更改framelayout中片段的按钮行。

<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" >

    <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:background="@drawable/button_home_define"
            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:background="@drawable/button_general_define"
            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:background="@drawable/button_general_define"
            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:background="@drawable/button_general_define"
            android:text="3" />
    </LinearLayout>

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

</LinearLayout>

GuitarActivity.java

这包含通过单击上面的xml中的按钮来拉入片段的代码。

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();
        }

        }

    }

fragment_guitar_tune_01.xml

这是一个片段,带有一排按钮来播放声音。

<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" >

    <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:background="@drawable/button_general_define"
        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:background="@drawable/button_general_define"
        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:background="@drawable/button_general_define"
        android:text="@string/guitar_note_03" />

</LinearLayout>

GuitarTune01Activity.java

这个java if片段。它是为片段中的一行按钮播放不同的声音。此代码不适用于片段,但在单独测试时有效。

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();
                    }
                });

            }
        });

    }

}

更改

我已经提出了更改建议并在纠正了一些错误(放置view.findViewbyID)后,我只有一个错误需要纠正才能测试它。

错误在线: MediaPlayer mp = MediaPlayer.create(getApplicationContext(),

它说

在文件中重命名(ctrl + 2,r)

无法从类型contextwrapper

中对非静态方法getapplicationcontext进行静态引用

有什么建议吗?感谢

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);
        }

    }

2 个答案:

答案 0 :(得分:0)

好的布局你需要改变这个:

<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" >

有:

<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="wrap_content"**
android:gravity="center_horizontal"
android:orientation="horizontal"
android:paddingBottom="30dp" >

您正在使用AppCompat库(扩展ActionBarActivity),您需要在清单文件中使用此主题 改变这个:

android:theme="@style/AppTheme"

有:

android:theme="@style/Theme.AppCompat" 

试试!!!

答案 1 :(得分:0)

这里没有一个简单的代码问题,我们可以简单地指出并帮助您修复;它更多的是对Android生命周期对象的误解:片段和活动是不可互换的。如果您要发布GuitarTune01Activity Activity而不是Fragment,则需要通过某些代码中的Intent作为活动启动它(例如,按钮)在你的一个片段中。

如果您希望能够像Fragment一样进出交换,那么您需要将其扩展Fragment而不是ActionBarActivity并相应地重写它。