Advertisement
Guest User

Untitled

a guest
Apr 24th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.43 KB | None | 0 0
  1. 03-20 16:11:17.055: E/AndroidRuntime(828): FATAL EXCEPTION: main
  2. 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
  3. 03-20 16:11:17.055: E/AndroidRuntime(828): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
  4. 03-20 16:11:17.055: E/AndroidRuntime(828): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
  5. 03-20 16:11:17.055: E/AndroidRuntime(828): at android.app.ActivityThread.access$600(ActivityThread.java:122)
  6. 03-20 16:11:17.055: E/AndroidRuntime(828): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
  7. 03-20 16:11:17.055: E/AndroidRuntime(828): at android.os.Handler.dispatchMessage(Handler.java:99)
  8. 03-20 16:11:17.055: E/AndroidRuntime(828): at android.os.Looper.loop(Looper.java:137)
  9. 03-20 16:11:17.055: E/AndroidRuntime(828): at android.app.ActivityThread.main(ActivityThread.java:4340)
  10. 03-20 16:11:17.055: E/AndroidRuntime(828): at java.lang.reflect.Method.invokeNative(Native Method)
  11. 03-20 16:11:17.055: E/AndroidRuntime(828): at java.lang.reflect.Method.invoke(Method.java:511)
  12. 03-20 16:11:17.055: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  13. 03-20 16:11:17.055: E/AndroidRuntime(828): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  14. 03-20 16:11:17.055: E/AndroidRuntime(828): at dalvik.system.NativeStart.main(Native Method)
  15. 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
  16. 03-20 16:11:17.055: E/AndroidRuntime(828): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
  17. 03-20 16:11:17.055: E/AndroidRuntime(828): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
  18. 03-20 16:11:17.055: E/AndroidRuntime(828): ... 11 more
  19.  
  20. public class FoursFragment extends Fragment {
  21.  
  22. private DbHelper mHelper;
  23. private SQLiteDatabase dataBase;
  24.  
  25. private ArrayList<String> userId = new ArrayList<String>();
  26. private ArrayList<String> user_fName = new ArrayList<String>();
  27. private ArrayList<String> user_lName = new ArrayList<String>();
  28.  
  29. private ListView userList;
  30. private AlertDialog.Builder build;
  31.  
  32. @Override
  33. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  34. Bundle savedInstanceState) {
  35. View view = inflater.inflate(R.layout.display_activity, container, false);
  36.  
  37. userList = (ListView) view.findViewById(R.id.List);
  38.  
  39. mHelper = new DbHelper(getActivity());
  40.  
  41. //add new record
  42. view.findViewById(R.id.btnAdd).setOnClickListener(new OnClickListener() {
  43.  
  44. public void onClick(View v) {
  45.  
  46. Intent i = new Intent(getActivity(),
  47. AddActivity.class);
  48. i.putExtra("update", false);
  49. startActivity(i);
  50.  
  51. }
  52. });
  53.  
  54. //click to update data
  55. userList.setOnItemClickListener(new OnItemClickListener() {
  56.  
  57. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
  58. long arg3) {
  59.  
  60. Intent i = new Intent(getActivity(),
  61. AddActivity.class);
  62. i.putExtra("Fname", user_fName.get(arg2));
  63. i.putExtra("Lname", user_lName.get(arg2));
  64. i.putExtra("ID", userId.get(arg2));
  65. i.putExtra("update", true);
  66. startActivity(i);
  67.  
  68. }
  69. });
  70.  
  71. //long click to delete data
  72. userList.setOnItemLongClickListener(new OnItemLongClickListener() {
  73.  
  74. public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
  75. final int arg2, long arg3) {
  76.  
  77. build = new AlertDialog.Builder(getActivity());
  78. build.setTitle("Delete " + user_fName.get(arg2) + " "
  79. + user_lName.get(arg2));
  80. build.setMessage("Do you want to delete ?");
  81. build.setPositiveButton("Yes",
  82. new DialogInterface.OnClickListener() {
  83.  
  84. public void onClick(DialogInterface dialog,
  85. int which) {
  86.  
  87. Toast.makeText(
  88. getActivity(),
  89. user_fName.get(arg2) + " "
  90. + user_lName.get(arg2)
  91. + " is deleted.", 3000).show();
  92.  
  93. dataBase.delete(
  94. DbHelper.TABLE_NAME,
  95. DbHelper.KEY_ID + "="
  96. + userId.get(arg2), null);
  97. displayData();
  98. dialog.cancel();
  99. }
  100. });
  101.  
  102. build.setNegativeButton("No",
  103. new DialogInterface.OnClickListener() {
  104.  
  105. public void onClick(DialogInterface dialog,
  106. int which) {
  107. dialog.cancel();
  108. }
  109. });
  110. AlertDialog alert = build.create();
  111. alert.show();
  112.  
  113. return true;
  114. }
  115. });
  116. return view;
  117. }
  118.  
  119. @Override
  120. public void onResume() {
  121. displayData();
  122. super.onResume();
  123. }
  124.  
  125. /**
  126. * displays data from SQLite
  127. */
  128. private void displayData() {
  129. dataBase = mHelper.getWritableDatabase();
  130. Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
  131. + DbHelper.TABLE_NAME, null);
  132.  
  133. userId.clear();
  134. user_fName.clear();
  135. user_lName.clear();
  136. if (mCursor.moveToFirst()) {
  137. do {
  138. userId.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID)));
  139. user_fName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_FNAME)));
  140. user_lName.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_LNAME)));
  141.  
  142. } while (mCursor.moveToNext());
  143. }
  144. DisplayAdapter disadpt = new DisplayAdapter(getActivity(),userId, user_fName, user_lName);
  145. userList.setAdapter(disadpt);
  146. mCursor.close();
  147. }
  148. }
  149.  
  150. public class AddActivity extends Fragment implements OnClickListener {
  151. private Button btn_save;
  152. private EditText edit_first,edit_last;
  153. private DbHelper mHelper;
  154. private SQLiteDatabase dataBase;
  155. private String id,fname,lname;
  156. private boolean isUpdate;
  157.  
  158. @Override
  159. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  160. Bundle savedInstanceState) {
  161. View view = inflater.inflate(R.layout.add_activity, container, false);
  162.  
  163.  
  164. btn_save=(Button) view.findViewById(R.id.save_btn);
  165. edit_first=(EditText)view.findViewById(R.id.frst_editTxt);
  166. edit_last=(EditText)view.findViewById(R.id.last_editTxt);
  167.  
  168. isUpdate=getActivity().getIntent().getExtras().getBoolean("update");
  169. if(isUpdate)
  170. {
  171. id=getActivity().getIntent().getExtras().getString("ID");
  172. fname=getActivity().getIntent().getExtras().getString("Fname");
  173. lname=getActivity().getIntent().getExtras().getString("Lname");
  174. edit_first.setText(fname);
  175. edit_last.setText(lname);
  176.  
  177. }
  178.  
  179. btn_save.setOnClickListener(this);
  180.  
  181. mHelper=new DbHelper(getActivity());
  182. return view;
  183.  
  184. }
  185.  
  186. // saveButton click event
  187. public void onClick(View v) {
  188. fname=edit_first.getText().toString().trim();
  189. lname=edit_last.getText().toString().trim();
  190. if(fname.length()>0 && lname.length()>0)
  191. {
  192. saveData();
  193. }
  194. else
  195. {
  196. AlertDialog.Builder alertBuilder=new AlertDialog.Builder(getActivity());
  197. alertBuilder.setTitle("Invalid Data");
  198. alertBuilder.setMessage("Please, Enter valid data");
  199. alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  200.  
  201. public void onClick(DialogInterface dialog, int which) {
  202. dialog.cancel();
  203.  
  204. }
  205. });
  206. alertBuilder.create().show();
  207. }
  208.  
  209. }
  210.  
  211. /**
  212. * save data into SQLite
  213. */
  214. private void saveData(){
  215. dataBase=mHelper.getWritableDatabase();
  216. ContentValues values=new ContentValues();
  217.  
  218. values.put(DbHelper.KEY_FNAME,fname);
  219. values.put(DbHelper.KEY_LNAME,lname );
  220.  
  221. System.out.println("");
  222. if(isUpdate)
  223. {
  224. //update database with new data
  225. dataBase.update(DbHelper.TABLE_NAME, values, DbHelper.KEY_ID+"="+id, null);
  226. }
  227. else
  228. {
  229. //insert data into database
  230. dataBase.insert(DbHelper.TABLE_NAME, null, values);
  231. }
  232. //close database
  233. dataBase.close();
  234.  
  235. }
  236. }
  237.  
  238. Intent i = new Intent(getActivity(),
  239. AddActivity.class);
  240. i.putExtra("update", false);
  241. startActivity(i);
  242.  
  243. Intent i = new Intent(getActivity(),
  244. AddActivity.class);
  245. i.putExtra("Fname", user_fName.get(arg2));
  246. i.putExtra("Lname", user_lName.get(arg2));
  247. i.putExtra("ID", userId.get(arg2));
  248. i.putExtra("update", true);
  249. startActivity(i);
  250.  
  251. public class AddActivity extends Fragment implements OnClickListener {
  252.  
  253. Fragment newFragment = new ExampleFragment();
  254. FragmentTransaction transaction = getFragmentManager().beginTransaction();
  255.  
  256. // Replace whatever is in the fragment_container view with this fragment,
  257. // and add the transaction to the back stack
  258. transaction.replace(R.id.fragment_container, newFragment);
  259. transaction.addToBackStack(null);
  260.  
  261. // Commit the transaction
  262. transaction.commit();
  263.  
  264. Caused by: java.lang.ClassCastException: app.norman.tennis.fours.AddActivity cannot be cast to android.app.Activity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement