我是移動應用程序開發的新手。我正在致力於android studio
。我在mysql DB
中創建了兩個表格,其中一個是users
,另一個是activity
。現在我正在使用users
表。我的任務是製作一個app
,其中應該有一個dropdown
,並且所有的用戶名都會在其中。當用戶選擇任何名稱時,該用戶的id
將被顯示。Android無法在模擬器中啓動應用程序
爲此我創建了一個php
文件,其中我返回了json
結果。
下面是我php
代碼
$sql = "SELECT * FROM users";
$r = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($r)){
array_push($result,array(
'Id'=>$row['Id'],
'Name'=>$row['Name']
));
}
echo json_encode(array('result'=>$result));
mysqli_close($con);
下面是上述代碼
{"users":[{"Id":"1","Name":"Faisal"},{"Id":"2","Name":"Salman"},{"Id":"3","Name":"Asim"},{"Id":"4","Name":"Asad"},{"Id":"5","Name":"Mateen"}]}
現在對我的機器人部分移位的結果
下面是我Config file
public class Config {
//JSON URL
public static final String DATA_URL = "http://10.0.2.2:8000/MobileApp/index.php";
//Tags used in the JSON String
public static final String TAG_NAME = "Name";
public static final String TAG_ID = "Id";
//JSON array name
public static final String JSON_ARRAY = "users";
}
MainActivity
public class MainActivity extends AppCompatActivity implements Spinner.OnItemSelectedListener {
//Declaring an Spinner
private Spinner spinner;
//An ArrayList for Spinner Items
private ArrayList<String> users;
//JSON Array
private JSONArray result;
//TextViews to display details
private TextView textViewResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing the ArrayList
users = new ArrayList<String>();
//Initializing Spinner
spinner = (Spinner)findViewById(R.id.spinner);
//Adding an Item Selected Listener to our Spinner
//As we have implemented the class Spinner.OnItemSelectedListener to this class iteself
// we are passing this to setOnItemSelectedListener
spinner.setOnItemClickListener((AdapterView.OnItemClickListener) this);
//Initializing TextView
textViewResult = (TextView)findViewById(R.id.textView);
//This method will fetch the data from the URL
getData();
}
private void getData() {
//Creating a string request
StringRequest stringRequest = new StringRequest(Config.DATA_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray(Config.JSON_ARRAY);
//Calling method getStudents to get the students from the JSON Array
getUsers(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
private void getUsers(JSONArray j) {
//Traversing through all the items in the json array
for(int i =0; i<j.length(); i++)
{
try
{
//Getting json object
JSONObject json = j.getJSONObject(i);
//Adding the name of the student to array list
users.add(json.getString(Config.TAG_NAME));
} catch (JSONException e) {
e.printStackTrace();
}
}
//Setting adapter to show the items in the spinner
spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_dropdown_item, users));
}
private void getUsers() {
}
//Method to get student name of a particular position
private String getName(int position)
{
String name = "";
try
{
//Getting object of given index
JSONObject json = result.getJSONObject(position);
//Fetching name from that object
name = json.getString(Config.TAG_NAME);
} catch (JSONException e) {
e.printStackTrace();
}
return name;
}
//Method to get student Id of a particular position
private String getId (int postion)
{
String Id="";
try{
JSONObject json = result.getJSONObject(postion);
Id = json.getString(Config.TAG_ID);
} catch (JSONException e) {
e.printStackTrace();
}
return Id;
}
//this method will execute when we pic an item from the spinner
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Appending the values to textview for a selected item
textViewResult.append("Hi " + getName(position) + "Your ID is " + getId(position));
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
textViewResult.setText("");
}}
現在我正在運行使用10.0.2.2
模擬器的應用程序。當我運行應用程序的應用程序崩潰和logcat
我得到以下錯誤
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.accurat.webaccess/com.example.accurat.webaccess.MainActivity}: java.lang.ClassCastException: com.example.accurat.webaccess.MainActivity cannot be cast to android.widget.AdapterView$OnItemClickListener
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.ClassCastException: com.example.accurat.webaccess.MainActivity cannot be cast to android.widget.AdapterView$OnItemClickListener
at com.example.accurat.webaccess.MainActivity.onCreate(MainActivity.java:64)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
而且它在textViewResult = (TextView)findViewById(R.id.textView);
更新1
命中通過使用spinner.setOnItemClickListener(this);
我收到以下錯誤
我kn有很多問題,我已經看過他們,但仍然無法解決問題。
我不知道是什麼問題。任何幫助將不勝感激。
這行是你的錯誤:'spinner.setOnItemClickListener((AdapterView.OnItemClickListener)this);'。 'this'(你的MainActivity)沒有實現AdapterView.OnItemClickListener – 0xDEADC0DE
use:'spinner.setOnItemClickListener(this);' – rafsanahmad007