0

trying to run a fragment with a button and getting error after the execution:

Following this S.O. answer How to handle button clicks using the XML onClick within Fragments , but not working with me.

DetalhesFragment.java

package com.example.waitersoriginal;

import android.app.Fragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.TextView;

public class DetalhesFragment extends Fragment implements OnClickListener{

    TextView nomeEntrada,descrEntrada,valorEntrada,nmArm,dsArm,vlArm;
    String txtNome, txtDescr;
    View view,v;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.detalhes_fragment, container, false);

        Button mButton = (Button) view.findViewById(R.id.button1);

        nomeEntrada= (TextView) view.findViewById(R.id.textView1);
        descrEntrada= (TextView)view.findViewById(R.id.textView2);
        valorEntrada= (TextView)view.findViewById(R.id.textView3);

        mButton.setOnClickListener((android.view.View.OnClickListener) this);
        return view;
    }
        public void onClick(View v) {
            // TODO Auto-generated method stub

            switch (v.getId()) {
                case R.id.button1:

                    PedidosFragment array = (PedidosFragment)getFragmentManager().findFragmentById(R.id.fragment3); 
                    array.criaArray(txtNome,txtDescr);

                    break;
        }
 }              

    public void change(String txt, String txt1){
    //public void change(String txt, String txt1, String txt2){
    //public void change(String txt){
        nomeEntrada.setText(txt);
        descrEntrada.setText(txt1);
        txtNome = txt;
        txtDescr = txt1;
        //valorEntrada.setText(txt2);

    }  

}

But this row contains errors:

public class DetalhesFragment extends Fragment implements OnClickListener{

Error: The type DetalhesFragment must implement the inherited abstract method DialogInterface.OnClickListener.onClick(DialogInterface,int)

I've tried to implement the method above, no success.

Can somebody help me with this?

Ty!

EDIT:

detalhes_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="14dp"
        android:layout_y="14dp"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="455dp"
        android:layout_height="wrap_content"
        android:layout_x="14dp"
        android:layout_y="78dp"
        android:textSize="18dp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="170dp"
        android:layout_height="wrap_content"
        android:layout_x="285dp"
        android:layout_y="19dp"
        android:textSize="22dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="14dp"
        android:layout_y="395dp"
        android:text="Adicionar aos Pedidos"
        android:visibility="visible" />

</AbsoluteLayout>
Community
  • 1
  • 1
xxxxxx
  • 39
  • 1
  • 8

3 Answers3

1

Remove this code:

 mButton.setOnClickListener((android.view.View.OnClickListener) this);
    return view;
}
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
            case R.id.button1:

                PedidosFragment array = (PedidosFragment)getFragmentManager().findFragmentById(R.id.fragment3); 
                array.criaArray(txtNome,txtDescr);

                break;
    }

}

Replace this code...

 mButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             switch (v.getId()) {
                case R.id.button1:

                    PedidosFragment array = (PedidosFragment)getFragmentManager().findFragmentById(R.id.fragment3); 
                    array.criaArray(txtNome,txtDescr);

                    break;
        }
    });
    return view;
}
Manikandan K
  • 1,081
  • 1
  • 9
  • 17
  • Mani, thank you! Now the activity showed up! I'm sorry, but, my button isn't visible in the fragment. Can you see my XML? At first.. can I add a button in XML of a fragment? – xxxxxx Oct 25 '14 at 05:06
  • can you use linear layout? Button is visible. check now – Manikandan K Oct 25 '14 at 06:42
0

As the error says your have to implement (add in your class) a method with the name onClick with a parameter of type DialogInterface and another one of type int.

That is because your are implementing an interface which is a kind of contract. All methods of the interface you implement it is mandatory to you to create it on your class even if it has no code at all.

Like:

@Override
public void onClick(DialogInterface dialogInterface, int i){}

EDIT

Sorry, I just saw your code and see the the method is there what you need to do is to add the right parameters. Right now you are passing just a view.

Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
0

Can you use Linear Layout.

Why did use absolute layout?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" ><TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="14dp"
    android:layout_y="14dp"
    android:textSize="25dp" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="455dp"
    android:layout_height="wrap_content"
    android:layout_x="14dp"
    android:layout_y="78dp"
    android:textSize="18dp" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="170dp"
    android:layout_height="wrap_content"
    android:layout_x="285dp"
    android:layout_y="19dp"
    android:textSize="22dp" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="14dp"
    android:layout_y="395dp"
    android:text="Adicionar aos Pedidos"
    android:visibility="visible" />

</LinearLayout>`

Can You try this...

Manikandan K
  • 1,081
  • 1
  • 9
  • 17