Results 1 to 7 of 7
  1. #1
    saif is offline Member
    Join Date
    Jul 2012
    Posts
    17
    Rep Power
    0

    Default Help with my first alert dialog project

    Hello

    i was making my fiirst aler dialog project but it was giving me error

    here my main activity code

    Java Code:
    package com.alertdialog;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.AlertDialog.Builder;
    import android.app.Dialog;
    import android.content.DialogInterface;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    	
    	private static final int ALERT_DIALOG = 0;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        
            Button btn = (Button) findViewById(R.id.btn);
            
            btn.setOnClickListener(new OnClickListener() {
    			
    			public void onClick(View arg0) {
    				
    				showDialog(ALERT_DIALOG);
    			
    				
    			}
    		});
        
        }
    
    	@Override
    	protected Dialog onCreateDialog(int id) {
    		
    		Dialog dialog = null;
    		
    		switch (id) {
    		case ALERT_DIALOG:
    			
    			AlertDialog.Builder builder = new AlertDialog.Builder(this);
    			builder.setMessage("This is an alert message");
    			builder.setCancelable(false);
    			builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
    				
    				                 public void onClick(DialogInterface dialog, int which) {
    					
    					                 dialog.dismiss();
    					
    			                  	}
    			               }) ;
    			
    		           dialog = builder.create();
    			
    			break;
    
    		default:
    			return super.onCreateDialog(id);
    			
    		}
    		
    		// TODO Auto-generated method stub
    		
    		return dialog;
    	 }
        
       }

    and here is my layout main_activity.xml


    Java Code:
    <RelativeLayout 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" >
    
       <Button android:id="@+id/btn" android:text="Show Dialog"/>
        
        
    </RelativeLayout>

    i have test this code on emulator but it was saying me to fix error on project
    pls someone help me to fix this error

    thank you

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Help with my first alert dialog project

    It might be an idea to actually tell us what the error is?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    saif is offline Member
    Join Date
    Jul 2012
    Posts
    17
    Rep Power
    0

    Default Re: Help with my first alert dialog project

    Multiple markers at this line
    - The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new
    OnClickListener(){})
    - OnClickListener cannot be resolved to a type
    Java Code:
     btn.setOnClickListener(new OnClickListener() {

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Help with my first alert dialog project

    So either Android doesn't have a class/interface named OnClickListener, or you failed to import it.

    db

    edit From the error message, it looks like OnClickListener might be an inner or nested class of View.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Help with my first alert dialog project

    And here's the API for it.

    How people manage to code without having an API to hand I don't know.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    saif is offline Member
    Join Date
    Jul 2012
    Posts
    17
    Rep Power
    0

    Default Re: Help with my first alert dialog project

    thanks for the help but there is another error in my layout activity_main.xml

    error:[I18N] Hardcoded string "Show Dialog", should use @string resource

    Java Code:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
            
           <Button android:id="@+id/btn" android:text="Show Dialog"/>
            
            
    
        </LinearLayout>

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default Re: Help with my first alert dialog project

    You need to read up on application resources in whatever book/tutorial/website.
    The Android site I linked to above would probably be a good place as well.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Java Alert Script
    By harry2509 in forum New To Java
    Replies: 1
    Last Post: 04-05-2012, 05:21 PM
  2. Javascript Alert
    By mutuah in forum New To Java
    Replies: 4
    Last Post: 03-25-2010, 05:28 PM
  3. mouse click alert
    By amir in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2008, 10:42 PM
  4. Using Alert in MIDlet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-22-2007, 10:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •