Java Android无法在emulator中启动应用程序
我是手机应用开发的新手。我正在安卓工作室工作。我在Java Android无法在emulator中启动应用程序,java,php,android,mysql,android-studio,Java,Php,Android,Mysql,Android Studio,我是手机应用开发的新手。我正在安卓工作室工作。我在mysql数据库中创建了两个表,一个是users,另一个是activity。目前,我正在使用用户表。我的任务是制作一个应用程序,其中应该有一个下拉列表,所有用户名都会在其中。当用户选择任何名称时,将显示该用户的id 为此,我创建了一个php文件,其中返回了一个json结果 下面是我的php代码 $sql = "SELECT * FROM users"; $r = mysqli_query($con,$sql); $result = arra
mysql数据库中创建了两个表,一个是users
,另一个是activity
。目前,我正在使用用户
表。我的任务是制作一个应用程序
,其中应该有一个下拉列表
,所有用户名都会在其中。当用户选择任何名称时,将显示该用户的id
为此,我创建了一个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"}]}
现在转向我的android部分
下面是我的配置文件
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";
}
main活动
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("");
}}
然后点击textViewResult=(TextView)findViewById(R.id.TextView)代码>
更新1
通过使用spinner.setOnItemClickListener(此)代码>我得到下面的错误
我知道这方面有很多问题,我已经研究过了,但我仍然无法解决这个问题
我不知道是什么问题。任何帮助都将不胜感激。试试以下方法:
import android.widget.AdapterView.OnItemSelectedListener;
现在:
在你的活动中:
spinner.setOnItemSelectedListener(this)
您还必须覆盖以下方法:
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
//write your code
}
public void onNothingSelected(AdapterView<?> parent) {
}
public void已选择(AdapterView父视图、视图v、int位置、长id){
//编写代码
}
未选择公共无效(AdapterView父级){
}
试试这个:
import android.widget.AdapterView.OnItemSelectedListener;
现在:
在你的活动中:
spinner.setOnItemSelectedListener(this)
您还必须覆盖以下方法:
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
//write your code
}
public void onNothingSelected(AdapterView<?> parent) {
}
public void已选择(AdapterView父视图、视图v、int位置、长id){
//编写代码
}
未选择公共无效(AdapterView父级){
}
尝试导入android.widget.AdapterView.OnItemSelectedListener代码>
这是eclipse中的一个常见问题,如中所述,请尝试导入android.widget.AdapterView.OnItemSelectedListener代码>
这是eclipse中的一个常见问题,正如在中提到的,使用android studio很容易做到
步骤1:将光标放在红线字上(这里是“this”)
步骤2:按Alt+Enter键
步骤3:选择Make-OnItemSelectedListener
步骤4:按下下一个对话框上的“确定”按钮..完成
通常这类问题可以用谷歌搜索
首先我们要看一下日志。
第二,我们必须找出问题所在
在您的日志中,您可以看到由标记后跟异常引起的
这里是第64行的ClassCastException
谷歌搜索一下就知道了。干杯使用android studio非常容易
步骤1:将光标放在红线字上(这里是“this”)
步骤2:按Alt+Enter键
步骤3:选择Make-OnItemSelectedListener
步骤4:按下下一个对话框上的“确定”按钮..完成
通常这类问题可以用谷歌搜索
首先我们要看一下日志。
第二,我们必须找出问题所在
在您的日志中,您可以看到由标记后跟异常引起的
这里是第64行的ClassCastException
谷歌搜索一下就知道了。干杯这一行是您的错误:spinner.setOnItemClickListener((AdapterView.OnItemClickListener)This)代码>'此“(您的MainActivity)未实现AdapterView.OnItemClickListeneruse:spinner.setOnItemClickListener(此)代码>这一行是您的错误:spinner.setOnItemClickListener((AdapterView.OnItemClickListener)This)代码>'此“(您的MainActivity)未实现AdapterView.OnItemClickListeneruse:spinner.setOnItemClickListener(此)代码>