-3

I have a following snippet of codes. What I want to do is that when I click button1 just show the text. But eclipse suggest me to add onClick(DialogInterface dialog, int which) but then at btnOk.setOnClickListener(oclBtnOk); it gives me this error:

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener)

Here is my code:

TextView tvOut;
    Button btnOk;
    Button btnCancel;

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
        }

        tvOut = (TextView) findViewById(R.id.textView1);
        btnOk = (Button) findViewById(R.id.button1);
        btnCancel = (Button) findViewById(R.id.button2);


     // create click listener
        OnClickListener oclBtnOk = new OnClickListener() {
          public void onClick(View v) {
            // change text of the TextView (tvOut)
            tvOut.setText("Button OK clicked");
          }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }



        };
        // assign click listener to the OK button (btnOK)
        btnOk.setOnClickListener(oclBtnOk);
    }
Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
user2711681
  • 285
  • 7
  • 16

2 Answers2

1

You are importing the wrong OnClickListener class.

View.setOnClickListener() Takes a View.OnClickListener, not a DialogInterface.OnClickListener, which is what you have imported.

If you don't use the DialogInterface.OnClickListener elsewhere in this class, simply change your import statement to import android.view.View.OnClickListener.

If you do also use the DialogInterface.OnClickListener interface in your class, you will need to further qualify the class name here, like so:

View.OnClickListener oclBtnOk = new View.OnClickListener() {
    public void onClick(View v) {
        // change text of the TextView (tvOut)
        tvOut.setText("Button OK clicked");
    }
}

You should also remove the onClick(DialogInterface dialog, int which) method, as that is only defined for DialogInterface.OnClickListener.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • Ok I will try it soon. But what is this dialog interface is it for the pop-up – user2711681 Jun 18 '14 at 19:02
  • There are a number of Dialog classes that use it, such as [AlertDialog](http://developer.android.com/reference/android/app/AlertDialog.html). Usually it is used for callbacks when the user presses a button in a Dialog. – Bryan Herbst Jun 18 '14 at 19:06
  • I have put this codes View.OnClickListener oclBtnOk = new View.OnClickListener() { public void onClick(View v) { // change text of the TextView (tvOut) tvOut.setText("Button OK clicked"); } }; btnOk.setOnClickListener(oclBtnOk); and when I try to run my app it say unfortunately it stopped. Is a very simple code what could be wrong? – user2711681 Jun 18 '14 at 19:13
  • Your logcat should show a complete stacktrace that tells you exactly what went wrong. – Bryan Herbst Jun 18 '14 at 19:14
  • Yes I can see which cause I find it have so many lines in the logcat. When I comment this codes the app run so is definitely something wrong here. – user2711681 Jun 18 '14 at 19:15
  • It has these 06-18 15:15:52.662: E/AndroidRuntime(1587): FATAL EXCEPTION: main, 06-18 15:15:52.662: E/AndroidRuntime(1587): Process: com.example.my1, PID: 1587,06-18 15:15:52.662: E/AndroidRuntime(1587): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.my1/com.example.my1.MainActivity}: java.lang.NullPointerException,06-18 15:15:52.662: E/AndroidRuntime(1587): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195),06-18 15:15:52.662: E/AndroidRuntime(1587): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) – user2711681 Jun 18 '14 at 19:17
  • My guess is that `tvOut` is null. You can put a breakpoint at `tvOut.setText()` to verify this. Make sure that your `activity_main` layout contains a TextView with id `textView1`. Also check out [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Bryan Herbst Jun 18 '14 at 19:20
  • The tvOut have a value – user2711681 Jun 18 '14 at 19:23
  • And is that in your `activity_main` layout XML file? If so, then put a breakpoint in `onCreate` and step through your code until you find what is causing the NullPointerException. – Bryan Herbst Jun 18 '14 at 19:24
  • Yes its in my fragment_main.xml. I have now created toggle breakpoint. I run yet the same it stopped. – user2711681 Jun 18 '14 at 19:29
  • You just pointed out the problem- you said it is in your `fragment_main`. I asked if it was in `activity_main`. From what I see in your code, the `fragment_main` layout isn't being used at all. – Bryan Herbst Jun 18 '14 at 19:33
  • I am a bit lost now which layout is used cause I read the main just for reference is it? I tried to change to this now setContentView(R.layout.fragment_main); also the same. But why even with activity_main when I commented my codes all my button n text view appear? – user2711681 Jun 18 '14 at 19:40
  • It sounds like you need to create a new SO question that addresses your NullPointerException specifically. This is getting a bit too involved for the comment section. – Bryan Herbst Jun 18 '14 at 19:46
  • Ok I will create a new one now. Thank you. – user2711681 Jun 18 '14 at 19:49
0

Same problem : The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (DialogInterface.OnClickListener)

calButton.setOnClickListener(addClick);

Source:

import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressWarnings("deprecation")
public class MainActivity extends ActionBarActivity {

LinearLayout layout1;
EditText no1Text,no2Text;
Button calButton;
TextView answerText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    layout1=new LinearLayout(this);


    no1Text=new EditText(this);
    no2Text=new EditText(this);
    calButton=new Button(this);
    answerText=new TextView(this);

    answerText.setText("0");
    calButton.setText("Calculate");

    layout1.setOrientation(LinearLayout.VERTICAL);

    calButton.setOnClickListener(addClick);

    layout1.addView(no1Text);
    layout1.addView(no2Text);
    layout1.addView(calButton);
    layout1.addView(answerText);


    setContentView(layout1);
}


private OnClickListener addClick=new OnClickListener() 
{

    @Override
    public void onClick(DialogInterface dialog, int which) 
    {

        String firstStr=no1Text.getText().toString();
        String secondStr=no2Text.getText().toString();


        double firstNo=Double.parseDouble(firstStr);
        double secondNo=Double.parseDouble(secondStr);

        double sumNo=firstNo+secondNo;

        String sumStr=String.valueOf(sumNo);

        answerText.setText(sumStr);
    }
};
}
Vijay
  • 54
  • 1
  • 6