-3

I am trying to call a method after clicking on a Button but I'm getting this error: "Cannot make a static reference to a non-static method”

here is the code, the only problematic thing in the code is when I try to call reakcja(); in button Intencja. Can you please help me?

package com.example.buttonwork;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

     public class MainActivity extends ActionBarActivity {

    public void reakcja(){
        Intent i = new Intent(this, Intencja.class);
        startActivity(i);


    }





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.i("click", "On create!");
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        Log.i("click","Teraz chyba pierwszy zapis instancji");
        }

    }

    @Override
    public void onStop(){
    Log.i("click","onStop");
    super.onStop();
    }

    @Override
    public void onPause(){
    Log.i("click","onPause");
    super.onPause();
    }

    @Override
    public void onResume(){
    Log.i("click","onResume");
    super.onResume();
    }





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        Log.i("click","Pierwsze tworzenie menu?");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            //Toast.makeText(item.getContext(), "elo", Toast.LENGTH_SHORT).show();
            Log.i("click", "menu");
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */


        public static class PlaceholderFragment extends Fragment {






            Button but1;
            Button Intencja;
            TextView tv1;
            EditText et1;
            Button raf,kas;
            ToggleButton off;
            SeekBar sk1;
            int kto;
            CheckBox cb1,cb2;
            RatingBar rb1; 
            public PlaceholderFragment() {
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.fragment_main, container, false);
                Log.i("click","Tutaj wszystko sie dzieje");
                rb1 = (RatingBar) (rootView.findViewById(R.id.rb1));
                /*rb1.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Toast.makeText(v.getContext(), String.valueOf(rb1.getProgress()), Toast.LENGTH_SHORT).show();

                    }
                });*/
                rb1.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {

                    @Override
                    public void onRatingChanged(RatingBar ratingBar, float rating,
                            boolean fromUser) {
                        // TODO Auto-generated method stub
                        Toast.makeText(ratingBar.getContext(), String.valueOf(rating), Toast.LENGTH_SHORT).show();
                    }
                });

               Intencja = (Button) (rootView.findViewById(R.id.Intencja));
               Intencja.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    reakcja();
                }
            });

                cb1 = (CheckBox) (rootView.findViewById(R.id.cb1));

                cb2 = (CheckBox) (rootView.findViewById(R.id.cb2));

                off = (ToggleButton) (rootView.findViewById(R.id.toggleButton1));
               off.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), off.getText(), Toast.LENGTH_SHORT).show();

                }
            });

                sk1 = (SeekBar) (rootView.findViewById(R.id.seekBar1));
                sk1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress,
                            boolean fromUser) {
                        // TODO Auto-generated method stub
                        //Toast.makeText(seekBar.getContext(), String.valueOf(progress), Toast.LENGTH_SHORT).show();
                        tv1.setText(String.valueOf(progress));
                        return;
                    }
                });
                /*sk1.setOnSeekBarChangeListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Toast.makeText(v.getContext(), sk1.getProgress(), Toast.LENGTH_SHORT).show();
                    }
                });*/

                tv1 = (TextView) (rootView.findViewById(R.id.tv1));
            et1 = (EditText) (rootView.findViewById(R.id.et1));
            raf = (Button) (rootView.findViewById(R.id.button1));
            raf.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(v.getContext(), "Ralfo jest miszczem", Toast.LENGTH_SHORT).show();
                    kto = 1;
                    sk1.setProgress(50);
                }
            });
            kas = (Button) (rootView.findViewById(R.id.button2));
            kas.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Toast.makeText(v.getContext(), "Nie, to Ralfo jest miszczem", Toast.LENGTH_SHORT).show();
                    kto =2;
                    sk1.setProgress(50);
                }
            });


            but1 = (Button) (rootView.findViewById(R.id.b1));
                but1.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    tv1.setText(et1.getText().toString());
                    Toast.makeText(v.getContext(), String.valueOf(cb1.isChecked() && cb2.isChecked()), Toast.LENGTH_SHORT).show();
                    // wyświetla Toasta true albo false sprawdzając CheckBoxy i robiąc na nich koniunkcję
                }
            });
                return rootView;
            }
        }
        }
thejh
  • 44,854
  • 16
  • 96
  • 107
Ralfo
  • 11
  • 4
  • 1
    You are calling a non-`static` method from inside a `static` inner class; how would it know which instance of the outer class to use? – Oliver Charlesworth Apr 27 '14 at 13:16
  • 1
    By the way, please don't post **hundreds** of lines of code into your question. You should reduce this to the simplest possible example that demonstrates the issue. – Oliver Charlesworth Apr 27 '14 at 13:16
  • Thank you for redirecting me, I am following the questions that you pasted. Can you please just tell me what should I do to make that Intent to work? This is my first step with Intents and I bet the answer is pretty simple. Thank you :) – Ralfo Apr 27 '14 at 14:05

1 Answers1

0

PlaceholderFragment is a static class, so you cannot call non-static method reakcja() from it. Make reakcja() static or PlaceholderFragment non-static.

haxtron
  • 46
  • 8