This Exception mainly occur because when we create an action on click of a button or an action to be performed on start of an activity ,but we forget the write the code in the activity for which we wrote intent to go.
for example in a main.xml file we have 7 buttons.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="30dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TRIMMER RESORT"
android:textColor="#0060aa"
android:textSize="25dp"
android:textStyle="bold"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/menu"
android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="MENU" />
<Button
android:id="@+id/events"
android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="EVENTS" />
<Button
android:id="@+id/directions"
android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="DIRECTIONS" />
<Button
android:id="@+id/bktable"
android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="BOOK A TABLE" />
<Button
android:id="@+id/share"
android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SHARE" />
<Button
android:id="@+id/aboutus"
android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ABOUT US" />
<Button
android:id="@+id/contactus"
android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="CONTACT US" />
</LinearLayout>
</ScrollView>
we wrote the MainActivity
as:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener {
Button menu,events,share,direction,bookTable,aboutUs,contactUs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
menu=(Button)findViewById(R.id.menu);
events=(Button)findViewById(R.id.events);
share=(Button)findViewById(R.id.share);
direction=(Button)findViewById(R.id.directions);
bookTable=(Button)findViewById(R.id.bktable);
aboutUs=(Button)findViewById(R.id.aboutus);
contactUs=(Button)findViewById(R.id.contactus);
menu.setOnClickListener(this);
events.setOnClickListener(this);
events.setOnClickListener(this);
share.setOnClickListener(this);
direction.setOnClickListener(this);
bookTable.setOnClickListener(this);
aboutUs.setOnClickListener(this);
contactUs.setOnClickListener(this);
}
public void onClick(View v) {
if(v==menu)
{
Intent i=new Intent(MainActivity.this,menuDetailActivity.class);
startActivity(i);
}
else if(v==events)
{
Intent i=new Intent(MainActivity.this,eventDetailActivity.class);
startActivity(i);
}
else if(v==contactUs)
{
Intent i=new Intent(MainActivity.this,contacUsDetail.class);
startActivity(i);
}
else
{
}
// TODO Auto-generated method stub
}
}
for example in a main.xml file we have 7 buttons.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="30dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TRIMMER RESORT"
android:textColor="#0060aa"
android:textSize="25dp"
android:textStyle="bold"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/menu"
android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="MENU" />
<Button
android:id="@+id/events"
android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="EVENTS" />
<Button
android:id="@+id/directions"
android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="DIRECTIONS" />
<Button
android:id="@+id/bktable"
android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="BOOK A TABLE" />
<Button
android:id="@+id/share"
android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SHARE" />
<Button
android:id="@+id/aboutus"
android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ABOUT US" />
<Button
android:id="@+id/contactus"
android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="CONTACT US" />
</LinearLayout>
</ScrollView>
we wrote the MainActivity
as:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener {
Button menu,events,share,direction,bookTable,aboutUs,contactUs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
menu=(Button)findViewById(R.id.menu);
events=(Button)findViewById(R.id.events);
share=(Button)findViewById(R.id.share);
direction=(Button)findViewById(R.id.directions);
bookTable=(Button)findViewById(R.id.bktable);
aboutUs=(Button)findViewById(R.id.aboutus);
contactUs=(Button)findViewById(R.id.contactus);
menu.setOnClickListener(this);
events.setOnClickListener(this);
events.setOnClickListener(this);
share.setOnClickListener(this);
direction.setOnClickListener(this);
bookTable.setOnClickListener(this);
aboutUs.setOnClickListener(this);
contactUs.setOnClickListener(this);
}
public void onClick(View v) {
if(v==menu)
{
Intent i=new Intent(MainActivity.this,menuDetailActivity.class);
startActivity(i);
}
else if(v==events)
{
Intent i=new Intent(MainActivity.this,eventDetailActivity.class);
startActivity(i);
}
else if(v==contactUs)
{
Intent i=new Intent(MainActivity.this,contacUsDetail.class);
startActivity(i);
}
else
{
}
// TODO Auto-generated method stub
}
}
Now if we jsut create the the three activities cotactUsDetail,eventDetail and menudetail classesa and for get to write the implementation code there
we get
"CLASS CAST EXCEPTION"application force closes down
in order to resove it we need to write all the three java file ,register them in manifest file and set the content view to respective activity that is for every activity one xml file shoud be given.
like contact detail is given here
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class contacUsDetail extends Activity implements OnClickListener{
Button call,email;
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.contactusdetail);
call=(Button)findViewById(R.id.call);
email=(Button)findViewById(R.id.email);
call.setOnClickListener(this);
email.setOnClickListener(this);
}
public void onClick(View v) {
if(v==call)
{
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent);
}
else if(v==email)
{
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
startActivity(intent);
}
}
// TODO Auto-generated method stub
}
contact detail.xml is given here
<?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"
android:padding="30dp">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="CALL US"
android:textSize="15dp"
android:textColor="#0060aa"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/call" android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="9052531507" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EMAIL US"
android:textSize="15dp"
android:textColor="#0060aa"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/email" android:textColor="#0060aa"
android:textSize="15dp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="muza@cytrion.com" />
</LinearLayout>
No comments:
Post a Comment