Click here to Skip to main content
15,896,522 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i created simple example in android that let you enter Name of book,author of the book and number of this book i add on click event to the button and this is the code for main activity to send info to second activity.
this is the first activity code whole class.

package com.test2;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AutoCompleteTextView;

public class Test2Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
    }
    public void OnAddClick(View button)
    {
    AutoCompleteTextView Name = (AutoCompleteTextView) findViewById(R.id.Name);
    AutoCompleteTextView Author = (AutoCompleteTextView) findViewById(R.id.Author);
    AutoCompleteTextView Number = (AutoCompleteTextView) findViewById(R.id.Number);

    
    Intent intent=new Intent();
	intent.setClass(this,Result.class);
    intent.putExtra("BookTitle", Name.getText().toString());
    intent.putExtra("AuthorName",Author.getText().toString());
    intent.putExtra("Number",Number.getText().toString());
    startActivity(intent);
    }
      public void OnCancelClick(View button)
    {
    	Intent intent=new Intent();
    	intent.setComponent(new ComponentName(this,Result.class));
    	intent.putExtra("Cancel", "Cancel");
    	
    	startActivity(intent);
    	
    	
    	
    }
    }



and this the code that will recived the intent (the whole class).
package com.test2;

import android.app.Activity;
import android.os.Bundle;

import android.widget.TextView;

public class Result extends Activity{
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    	setContentView(R.layout.result);
		TextView resultText=(TextView) findViewById(R.id.resultText);
		Bundle bundle=getIntent().getExtras();
		if(bundle.getString("Cancel")!=null)
		{
			resultText.setText("Operation Cancelled");
		}
		else
			
		{
		String BookTitle=bundle.getString("BookTitle");
		String BookAuthor=bundle.getString("AuthorName");
		String BookNumber=bundle.getString("Number");
		resultText.setText(
				"the result is"+ BookTitle + "-" + BookAuthor + "-" + BookNumber);
		}
	}
}

and this button xml code:

<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.11" android:text="Add" android:onClick="OnAddClick"></Button>







when i run it it give unexpected error for add button , and for cancel button it work normally. what i miss in the code ? thanks.
Posted
Updated 21-Sep-11 18:03pm
v2
Comments
Niklas L 22-Sep-11 6:41am    
On what line does it fail? Step through the code in a debugger, and see which of your statements that fail.

1 solution

Hi,
Could you attatch full class.

I think ,the Intent variable is not wrok correctly.
 
Share this answer
 
Comments
hamzah1 22-Sep-11 0:04am    
I updated the question please review .
thanks.
JamesIntel 22-Sep-11 0:36am    
I have no deep knowledge with andorid.

But It seems be bundle don't get Button name.

Try put button name to bundle using putExtras manualy.Good Luck.
hamzah1 22-Sep-11 3:02am    
Nopi try to do this but it didnt work !!
hamzah1 22-Sep-11 14:35pm    
I solve this problem finally :P

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900