Reputation: 119
I have an activity class connected directly to the main fragment class. Once a button on that class is clicked, it is meant to take me to another class where details are input. The first class works but as soon as the button is clicked the following error is produced:
03-20 16:11:17.055: E/AndroidRuntime(828): FATAL EXCEPTION: main
03-20 16:11:17.055: E/AndroidRuntime(828): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{app.norman.tennis/app.norman.tennis.fours.AddActivity}: java.lang.ClassCastException: app.norman.tennis.fours.AddActivity cannot be cast to android.app.Activity
03-20 16:11:17.055: E/AndroidRuntime(828): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
03-20 16:11:17.055: E/AndroidRuntime(828): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
03-20 16:11:17.055: E/AndroidRuntime(828): at android.app.ActivityThread.access$600(ActivityThread.java:122)
03-20 16:11:17.055: E/AndroidRuntime(828): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
03-20 16:11:17.055: E/AndroidRuntime(828): at android.os.Handler.dispatchMessage(Handler.java:99)
03-20 16:11:17.055: E/AndroidRuntime(828): at android.os.Looper.loop(Looper.java:137)
03-20 16:11:17.055: E/AndroidRuntime(828): at android.app.ActivityThread.main(ActivityThread.java:4340)
03-20 16:11:17.055: E/AndroidRuntime(828): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 16:11:17.055: E/AndroidRuntime(828): at java.lang.reflect.Method.invoke(Method.java:511)
03-20 16:11:17.055: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-20 16:11:17.055: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-20 16:11:17.055: E/AndroidRuntime(828): at dalvik.system.NativeStart.main(Native Method)
03-20 16:11:17.055: E/AndroidRuntime(828): Caused by: java.lang.ClassCastException: app.norman.tennis.fours.AddActivity cannot be cast to android.app.Activity
03-20 16:11:17.055: E/AndroidRuntime(828): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
03-20 16:11:17.055: E/AndroidRuntime(828): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
03-20 16:11:17.055: E/AndroidRuntime(828): ... 11 more
Below is the first class that displays the button (FoursFragment):
public class FoursFragment extends Fragment {
private DbHelper mHelper;
private SQLiteDatabase dataBase;
private ArrayList<String> userId = new ArrayList<String>();
private ArrayList<String> user_fName = new ArrayList<String>();
private ArrayList<String> user_lName = new ArrayList<String>();
private ListView userList;
private AlertDialog.Builder build;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.display_activity, container, false);
userList = (ListView) view.findViewById(R.id.List);
mHelper = new DbHelper(getActivity());
//add new record
view.findViewById(R.id.btnAdd).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getActivity(),
AddActivity.class);
i.putExtra("update", false);
startActivity(i);
}
});
//click to update data
userList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i = new Intent(getActivity(),
AddActivity.class);
i.putExtra("Fname", user_fName.get(arg2));
i.putExtra("Lname", user_lName.get(arg2));
i.putExtra("ID", userId.get(arg2));
i.putExtra("update", true);
startActivity(i);
}
});
//long click to delete data
userList.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {
build = new AlertDialog.Builder(getActivity());
build.setTitle("Delete " + user_fName.get(arg2) + " "
+ user_lName.get(arg2));
build.setMessage("Do you want to delete ?");
build.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(
getActivity(),
user_fName.get(arg2) + " "
+ user_lName.get(arg2)
+ " is deleted.", 3000).show();
dataBase.delete(
DbHelper.TABLE_NAME,
DbHelper.KEY_ID + "="
+ userId.get(arg2), null);
displayData();
dialog.cancel();
}
});
build.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
AlertDialog alert = build.create();
alert.show();
return true;
}
});
return view;
}
@Override
public void onResume() {
displayData();
super.onResume();
}
/**
* displays data from SQLite
*/
private void displayData() {
dataBase = mHelper.getWritableDatabase();
Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
+ DbHelper.TABLE_NAME, null);
userId.clear();
user_fName.clear();
user_lName.clear();
if (mCursor.moveToFirst()) {
do {
userId.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID)));
user_fName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_FNAME)));
user_lName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_LNAME)));
} while (mCursor.moveToNext());
}
DisplayAdapter disadpt = new DisplayAdapter(getActivity(),userId, user_fName, user_lName);
userList.setAdapter(disadpt);
mCursor.close();
}
}
And now the activity that displays the error:
public class AddActivity extends Fragment implements OnClickListener {
private Button btn_save;
private EditText edit_first,edit_last;
private DbHelper mHelper;
private SQLiteDatabase dataBase;
private String id,fname,lname;
private boolean isUpdate;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.add_activity, container, false);
btn_save=(Button) view.findViewById(R.id.save_btn);
edit_first=(EditText)view.findViewById(R.id.frst_editTxt);
edit_last=(EditText)view.findViewById(R.id.last_editTxt);
isUpdate=getActivity().getIntent().getExtras().getBoolean("update");
if(isUpdate)
{
id=getActivity().getIntent().getExtras().getString("ID");
fname=getActivity().getIntent().getExtras().getString("Fname");
lname=getActivity().getIntent().getExtras().getString("Lname");
edit_first.setText(fname);
edit_last.setText(lname);
}
btn_save.setOnClickListener(this);
mHelper=new DbHelper(getActivity());
return view;
}
// saveButton click event
public void onClick(View v) {
fname=edit_first.getText().toString().trim();
lname=edit_last.getText().toString().trim();
if(fname.length()>0 && lname.length()>0)
{
saveData();
}
else
{
AlertDialog.Builder alertBuilder=new AlertDialog.Builder(getActivity());
alertBuilder.setTitle("Invalid Data");
alertBuilder.setMessage("Please, Enter valid data");
alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertBuilder.create().show();
}
}
/**
* save data into SQLite
*/
private void saveData(){
dataBase=mHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(DbHelper.KEY_FNAME,fname);
values.put(DbHelper.KEY_LNAME,lname );
System.out.println("");
if(isUpdate)
{
//update database with new data
dataBase.update(DbHelper.TABLE_NAME, values, DbHelper.KEY_ID+"="+id, null);
}
else
{
//insert data into database
dataBase.insert(DbHelper.TABLE_NAME, null, values);
}
//close database
dataBase.close();
}
}
This application works extending activity but my first time working with fragments has made me struggle.
If you would like any other information or classes please just let me know and i will provide it. Is it because it is not directly connected to the fragment and is being called from a follow-up class?.
Upvotes: 2
Views: 1642
Reputation: 47807
You have got this error log
Caused by: java.lang.ClassCastException: app.norman.tennis.fours.AddActivity cannot be cast to android.app.Activity
It's becoz AddActivity
is not a Activity
class. Its a Fragment
and you can not start a Fragment like activities.
Go to this more information: http://developer.android.com/reference/android/app/Fragment.html
Upvotes: 1
Reputation: 133560
Caused by: java.lang.ClassCastException: app.norman.tennis.fours.AddActivity cannot be cast to android.app.Activity
You have
Intent i = new Intent(getActivity(),
AddActivity.class);
i.putExtra("update", false);
startActivity(i);
And
Intent i = new Intent(getActivity(),
AddActivity.class);
i.putExtra("Fname", user_fName.get(arg2));
i.putExtra("Lname", user_lName.get(arg2));
i.putExtra("ID", userId.get(arg2));
i.putExtra("update", true);
startActivity(i);
And this
public class AddActivity extends Fragment implements OnClickListener {
AddActivity
is not a Activity class. Its a fragment and you cannot start a fragment like the way you do for activities.
You need to add the fragment to the container. Fragment
is hosted by a Activity
.
So in your Activity xml have a container and add or replace fragment to it.
Example from the docs
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
Upvotes: 2