Unexplained "error"


Last Updated:

  1. Nickvkaam

    Nickvkaam New Member This Topic's Starter

    Joined:
    Aug 20, 2010
    Messages:
    1
    Likes Received:
    0
    Hey Guys/gals,

    I was doing some tutorials, when I finished one i ran in to a problem.

    The code down here shows an error in Eclipse:

    btnDialog.setOnClickListener(new OnClickListener() {

    I got 3 of these listeners.

    The auto correct tools only suggests to change setOnClickListener to any other listener. I've searched the web a bit but didn't find an awnser.

    Bellow if the whole code:

    package app.weaselmummy.androidtest;

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.AlertDialog.Builder;
    import android.content.DialogInterface;
    import android.content.DialogInterface.OnClickListener;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;

    public class testApp extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Builder dialog = new AlertDialog.Builder(this);
    dialog.setTitle(R.string.dialog_title);
    dialog.setMessage(R.string.dialog_text);
    dialog.setPositiveButton(R.string.dialog_ok, null);
    dialog.setNegativeButton(R.string.dialog_cancel, null);

    Button btnDialog = (Button) findViewById(R.id.Button01);
    btnDialog.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    dialog.show();
    }
    });

    Button btnSound = (Button) findViewById(R.id.Button02);
    btnSound.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    MediaPlayer player = new MediaPlayer();
    try {
    player.setDataSource(getString(R.string.sound_file));
    player.prepare();
    player.start();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });

    Button btnRelative = (Button) findViewById(R.id.Button03);
    btnRelative.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    Intent i = new Intent(testApp.this, Relative.class);
    startActivity(i);
    finish();
    }
    });
    }
    }


    Would appreciate any help!
     

    Advertisement
  2. andywhoa

    andywhoa Well-Known Member

    Joined:
    May 15, 2010
    Messages:
    105
    Likes Received:
    13
    In your imports list you have:
    Code (Text):
    1. import android.content.DialogInterface.OnClickListener;
    Remove this and replace it with:
    Code (Text):
    1. import android.view.View.OnClickListener;
     
  3. santoshmadalli

    santoshmadalli New Member

    Joined:
    Aug 24, 2010
    Messages:
    2
    Likes Received:
    0
    you can implement onClickListener and then implement onClick(Veiw v) method.

    Below is the code for your reference,

    import android.app.Activity;
    import android.os.Bundle;
    import android.content.Intent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;

    public class Sudoku extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Set up click listeners for all the buttons
    View continueButton = findViewById(R.id.continue_button);
    continueButton.setOnClickListener(this);
    View newButton = findViewById(R.id.new_button);
    newButton.setOnClickListener(this);
    View aboutButton = findViewById(R.id.about_button);
    aboutButton.setOnClickListener(this);
    View exitButton = findViewById(R.id.exit_button);
    exitButton.setOnClickListener(this);
    }

    public void onClick(View v) {
    switch (v.getId()) {
    case R.id.about_button:
    Intent i = new Intent(this, About.class);
    startActivity(i);
    break;
    // More buttons go here (if any) ...
    }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.settings:
    startActivity(new Intent(this, Prefs.class));
    return true;
    // More items go here (if any) ...
    }
    return false;
    }
    }
     

Share This Page

Loading...