我正在尝试从外部数据库填充我的应用程序中的所有微调器。 问题是我的应用程序粉碎了代码行:
cat_sp_add=(Spinner)findViewById(R.id.categories_add_new_product);
即时通讯使用正如你在代码中看到的方法runOnUiThread但仍然是它的结果。这是代码。有人可以告诉我,我做错了什么?
package com.example.zygy1;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class Load_lists extends Activity implements Runnable {
final String TAG_SUCCESS = "success";
final String TAG_CITIES = "cities";
final String TAG_CATEGORIES = "categories";
final String TAG_NAME = "name";
final String url_load_lists = "http://zygy.co.il/lib/android_connect/load_lists.php";
boolean error=false;
JSONParser jParser = new JSONParser();
ArrayList<String> CategoriesList;
ArrayList<String> CitiesList;
JSONArray categories = null;
JSONArray cities = null;
ArrayAdapter<String> adapter;
static Spinner cities_sp_add;
static Spinner cities_sp_signUp;
static Spinner cat_sp_add;
static Spinner cat_sp_search;
@Override
public void run() {
runOnUiThread(new Runnable(){
public void run(){
ArrayList<Product> productList_final=new ArrayList<Product>();
cat_sp_add=(Spinner)findViewById(R.id.categories_add_new_product);
cat_sp_search=(Spinner)findViewById(R.id.categories_search);
cities_sp_add=(Spinner) findViewById(R.id.cities_add_new_product);
cities_sp_signUp=(Spinner) findViewById(R.id.cities_signUp);
CategoriesList = new ArrayList<String>();
CitiesList = new ArrayList<String>();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(TAG_CITIES,"true"));
params.add(new BasicNameValuePair(TAG_CATEGORIES,"true"));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_load_lists, "GET", params);
// Check your log cat for JSON reponse
Log.d("Add new product ", json.toString());
// getting lists from database
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
categories=json.getJSONArray(TAG_CATEGORIES);
for (int i = 0; i < categories.length(); i++) {
JSONObject c = categories.getJSONObject(i);
String cat_name = new String();
cat_name=c.getString(TAG_NAME);
CategoriesList.add(cat_name);
}
cities=json.getJSONArray(TAG_CITIES);
for (int i = 0; i < cities.length(); i++) {
JSONObject c = cities.getJSONObject(i);
String city_name = new String();
city_name=c.getString(TAG_NAME);
CitiesList.add(city_name);
}
}
else {
// no match found
error=true;
}
} catch (JSONException e) {
e.printStackTrace();
}
// set lists in all application
if(!error){
adapter = new ArrayAdapter<String>(getApplicationContext(),
R.layout.spinner_text,CategoriesList);
adapter.setDropDownViewResource(R.layout.spinner_text);
cat_sp_add.setAdapter(adapter);
// cat_sp_search.setAdapter(adapter);
adapter = new ArrayAdapter<String>(getApplicationContext(),
R.layout.spinner_text,CitiesList);
adapter.setDropDownViewResource(R.layout.spinner_text);
cities_sp_add.setAdapter(adapter);
// cities_sp_signUp.setAdapter(adapter);
}
else {
Toast.makeText(getApplicationContext(), "error in uploading lists", Toast.LENGTH_LONG).show();
}
}});
}
}
激活线程的主类:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Load_lists load=new Load_lists();
Thread load_lists=new Thread(load);
load_lists.start();
setContentView(R.layout.activity_log_in);
bt_log_in=(Button)findViewById(R.id.button_log_in);
bt_sign_up=(Button)findViewById(R.id.button_sign_up);
....contine }
答案 0 :(得分:0)
尝试从onCreate中的线程中解析变量。
确保将它们设为最终,然后您可以在线程中访问它。
原因是,启动Thread时未加载Layout。您可能还想在启动Thread之前尝试使用setContentView()。