Advertisement
Guest User

mainactivity and error

a guest
Jan 25th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.10 KB | None | 0 0
  1. package com.example.mysqlexampleproject;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.res.Resources;
  8. import android.os.AsyncTask;
  9. import android.os.Bundle;
  10. import android.view.View;
  11. import android.widget.Adapter;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.Button;
  14. import android.widget.ListView;
  15. import android.widget.TextView;
  16.  
  17. import org.json.JSONArray;
  18.  
  19. import java.sql.Array;
  20. import java.sql.Connection;
  21. import java.sql.DriverManager;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.ArrayList;
  26. import java.util.HashMap;
  27. import java.util.LinkedHashMap;
  28. import java.util.Map;
  29.  
  30. public class MainActivity extends AppCompatActivity {
  31.  
  32. //googleBtn.setOnClickListener(new View.OnClickListener() {
  33. //
  34. // @Override
  35. // public void onClick(View v) {
  36. // //String google = "https://www.google.com";
  37. // //Uri webaddress = Uri.parse(google);
  38.  
  39. //Intent gotoGoogle = new Intent(Intent.ACTION_VIEW, webaddress);
  40. //if (gotoGoogle.resolveActivity(getPackageManager()) != null) {
  41. // startActivity(gotoGoogle);
  42. //}
  43. // }
  44. // });
  45.  
  46. //}
  47.  
  48. ArrayAdapter ArrayAdapter;
  49. // Construct the data source
  50. ArrayList<user> arrayOfUsers = new ArrayList<user>();
  51. // Create the adapter to convert the array to views
  52. UsersAdapter adapter = new UsersAdapter(this, arrayOfUsers);
  53. // Attach the adapter to a ListView
  54. ListView listView = (ListView) findViewById(R.id.myListView);
  55. //listView.setAdapter(ArrayAdapter);
  56. Context thisContext;
  57. ListView myListView;
  58. TextView progressTextView;
  59. Integer resource;
  60. //Map<Integer, String> usersMap = new LinkedHashMap<Integer, String>(); //A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value.
  61. //String[][] userList;
  62.  
  63. @Override
  64. protected void onCreate(Bundle savedInstanceState) {
  65. super.onCreate(savedInstanceState);
  66. setContentView(R.layout.activity_main);
  67.  
  68. //Attempts to launch an activity within our own app
  69. //Button secondActivityBtn = (Button)findViewById(R.id.loginBtn);
  70. //secondActivityBtn.setOnClickListener(new View.OnClickListener() {
  71.  
  72. // @Override
  73. // public void onClick(View v_) {
  74. // Intent startIntent = new Intent(getApplicationContext(), loginScreen.class);
  75. // //show how to pass info to another activity
  76. // startIntent.putExtra("com.example.mysqlexampleproject.NAME", "TEST");
  77. // startActivity(startIntent);
  78. // }
  79. //});
  80.  
  81. //Attempt to launch an activity outside our app
  82. //Button googleBtn = (Button)findViewById(R.id.signUpBtn);
  83.  
  84. Resources res = getResources();
  85. myListView = (ListView) findViewById(R.id.myListView);
  86. progressTextView = (TextView) findViewById(R.id.progressTextView);
  87. thisContext = this;
  88. listView.setAdapter(adapter);
  89.  
  90. progressTextView.setText("");
  91. Button btn = (Button) findViewById(R.id.getDataButton);
  92. btn.setOnClickListener(new View.OnClickListener() {
  93. @Override
  94. public void onClick(View v) {
  95. GetData retrieveData = new GetData();
  96. retrieveData.execute("");
  97. }
  98. });
  99. }
  100.  
  101. private class GetData extends AsyncTask<String,String,String> {
  102.  
  103. Integer flag = 0;
  104. //Integer user = 0;
  105. String msg = "";
  106. //JDBC driver name and database URL
  107. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  108. // Example: 10.20.30.40:3306
  109. static final String DB_URL = "jdbc:mysql://" + DBStrings.DATABASE_URL + "/" + DBStrings.DATABASE_NAME;
  110.  
  111. @Override
  112. protected void onPreExecute() {
  113. progressTextView.setText("Connecting to database...");
  114. }
  115.  
  116. @Override
  117. protected String doInBackground(String... params) {
  118.  
  119. Connection conn = null;
  120. Statement stmt = null;
  121.  
  122. try {
  123.  
  124. flag = 1;
  125. Class.forName(JDBC_DRIVER);
  126. conn = DriverManager.getConnection(DB_URL, DBStrings.USERNAME, DBStrings.PASSWORD);
  127. //conn = DriverManager.getConnection(DB_URL);
  128.  
  129. flag = 2;
  130. stmt = conn.createStatement();
  131. String sql = "SELECT * FROM users";
  132. ResultSet rs = stmt.executeQuery(sql);
  133.  
  134. //lets hope this works
  135. ArrayList<user> users = new ArrayList<>();
  136.  
  137. /*
  138. // Add item to adapter
  139. User newUser = new User("Nathan", "San Diego");
  140. adapter.add(newUser);
  141. // Or even append an entire new collection
  142. // Fetching some data, data has now returned
  143. // If data was JSON, convert to ArrayList of User objects.
  144. JSONArray jsonArray = ...;
  145. ArrayList<User> newUsers = User.fromJson(jsonArray)
  146. adapter.addAll(newUsers);
  147. */
  148.  
  149.  
  150. flag = 3;
  151. while (rs.next()) {
  152. Integer userID = rs.getInt("userID");
  153. String firstName = rs.getString("firstName");
  154. String surname = rs.getString("surname");
  155. String username = rs.getString("username");
  156. user newUser = new user(username, userID);
  157. newUser.user_id = userID;
  158. newUser.username = username;
  159. //UsersAdapter.add(newUser);
  160. adapter.add(newUser);
  161. //Double price = rs.getDouble("price");
  162. //userList[user][0] = firstName;
  163. //userList[user][1] = surname;
  164. //userList[user][2] = username;
  165. //usersMap.put(userID, firstName);
  166. //usersMap.put(name, price);
  167. //user += 1;
  168. flag = 4;
  169. }
  170.  
  171. msg = "Process complete.";
  172.  
  173. rs.close();
  174. stmt.close();
  175. conn.close();
  176.  
  177.  
  178. //milbs recommends you create static array list of users in user class (or new class)
  179. //ArrayList<user> users = new ArrayList<>();
  180. //user newUser = new user();
  181. //newUser.user_id = 5;
  182. //newUser.username = "TJM4";
  183. //users.add(newUser);
  184.  
  185. //users.contains(newUser);
  186.  
  187.  
  188. //milbs recommends you dont use these
  189. //HashMap<String, user> newHashmap = new HashMap<>();
  190. //newHashmap.put("new user string", newUser);
  191.  
  192. //user userfromhashmap = newHashmap.get("new user string");
  193.  
  194.  
  195.  
  196. } catch (SQLException connError) {
  197. msg = "An exception was thrown for JDBC.\nFlag: " + flag.toString();
  198. connError.printStackTrace();
  199. } catch (ClassNotFoundException e) {
  200. msg = "A class not found exception was thrown.";
  201. e.printStackTrace();
  202. } finally {
  203.  
  204. try {
  205. if (stmt != null) {
  206. stmt.close();
  207. }
  208. } catch (SQLException e) {
  209. e.printStackTrace();
  210. }
  211.  
  212. try {
  213. if (conn != null) {
  214. conn.close();
  215. }
  216. } catch (SQLException e) {
  217. e.printStackTrace();
  218. }
  219.  
  220. }
  221.  
  222. return null;
  223. }
  224.  
  225. @Override
  226. protected void onPostExecute(String msg) {
  227.  
  228. progressTextView.setText(this.msg);
  229.  
  230. //if (userList.length() > 0) {
  231. // itemAdapter = new ItemAdapter(thisContext, userList);
  232. //myListView.setAdapter(itemAdapter);
  233. //}
  234.  
  235. //if (usersMap.size() > 0) {
  236. if (arrayOfUsers.size() > 0) {
  237.  
  238. //itemAdapter = new ItemAdapter(thisContext, usersMap);
  239. ArrayAdapter = new ArrayAdapter(thisContext, resource);
  240. myListView.setAdapter(ArrayAdapter);
  241. }
  242. }
  243. }
  244.  
  245. } //END OF MAINACTIVITY
  246.  
  247. //////ERROR//////
  248. E/AndroidRuntime: FATAL EXCEPTION: main
  249. Process: com.example.mysqlexampleproject, PID: 7036
  250. java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mysqlexampleproject/com.example.mysqlexampleproject.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
  251. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2679)
  252. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
  253. at android.app.ActivityThread.-wrap11(Unknown Source:0)
  254. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
  255. at android.os.Handler.dispatchMessage(Handler.java:106)
  256. at android.os.Looper.loop(Looper.java:164)
  257. at android.app.ActivityThread.main(ActivityThread.java:6494)
  258. at java.lang.reflect.Method.invoke(Native Method)
  259. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
  260. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
  261. Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
  262. at android.app.Activity.getSystemService(Activity.java:5915)
  263. at android.view.LayoutInflater.from(LayoutInflater.java:229)
  264. at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:210)
  265. at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:204)
  266. at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:190)
  267. at com.example.mysqlexampleproject.UsersAdapter.<init>(UsersAdapter.java:18)
  268. at com.example.mysqlexampleproject.MainActivity.<init>(MainActivity.java:52)
  269. at java.lang.Class.newInstance(Native Method)
  270. at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
  271. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
  272. at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
  273. at android.app.ActivityThread.-wrap11(Unknown Source:0) 
  274. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
  275. at android.os.Handler.dispatchMessage(Handler.java:106) 
  276. at android.os.Looper.loop(Looper.java:164) 
  277. at android.app.ActivityThread.main(ActivityThread.java:6494) 
  278. at java.lang.reflect.Method.invoke(Native Method) 
  279. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
  280. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
  281. Process 7036 terminated.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement