我是android工作室的新手。我的项目在eclipse IDE上工作正常。我刚刚将eclipse项目导入 android studio 。 我试图将状态加载到微调器中。微调器在对话框中。我正在使用AsyncTask来获取状态并将它们填充到微调器中。相同的代码在eclipse中正常工作,但在android studio中它给出了错误
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import NWpackageHyp.AccessNwData;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class Login extends Activity
{
EditText edusername,edpassword;
Button registration;
String username,password,state;
int userid=0;
ProgressDialog pDialog;
boolean status=false;
Spinner spinner;
ArrayAdapter<String> spinnerAdapter;
ConnectionDetector Internet;
List<String> stateList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Internet=new ConnectionDetector(getApplicationContext());
edusername=(EditText)findViewById(R.id.edtxtusername);
edpassword=(EditText)findViewById(R.id.edtxtpassword);
registration=(Button)findViewById(R.id.btnRegister);
final GetState asyncstate=new GetState();
if(!Internet.isConnectingToInternet())
{
Toast.makeText(getApplicationContext(), "Please Check Internet Connection!!", Toast.LENGTH_LONG).show();
}
else
{
asyncstate.execute();
}
registration.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(Internet.isConnectingToInternet())
{
showDialog(0);
asyncstate.execute();
}
else
{
Toast.makeText(getApplicationContext(), "Please Check Internet Connection!!", Toast.LENGTH_LONG).show();
}
}
});
}
public Dialog onCreateDialog(int id)
{
final Dialog builder=new Dialog(this);
switch(id)
{
case 0:
builder.setContentView(R.layout.regdialog);
builder.setTitle("Registeration");
spinner = (Spinner)builder.findViewById(R.id.spnState);
final EditText username=(EditText)builder.findViewById(R.id.edtxtregusername);
final EditText password=(EditText)builder.findViewById(R.id.edtxtregpassword);
final EditText repassword=(EditText)builder.findViewById(R.id.edtxtrepassword);
spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, stateList);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
Button registernow=(Button)builder.findViewById(R.id.btnRegisternow);
registernow.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
final String uname,pass,repass,statestr;
statestr=(String) spinner.getSelectedItem();
uname=username.getText().toString();
pass=password.getText().toString();
repass=repassword.getText().toString();
if(!statestr.equals("") && !uname.equals("") && !pass.equals(""))
{
if(pass.equals(repass))
{
//Toast.makeText(getApplicationContext(),statestr+" "+uname+" "+pass+" "+repass , Toast.LENGTH_LONG).show();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("run ");
AccessNwData nwobj=new AccessNwData();
try{
String res=nwobj.registerUser(statestr, uname, pass);
System.out.println("soham "+res);
JSONArray jlogin = new JSONArray(res);
JSONObject regObj = (JSONObject) jlogin.getJSONObject(0);
if(regObj.getString("status").equals("true"))
{
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Login.this, "Registred Successfully You Can Login Now ", Toast.LENGTH_LONG).show();
username.setText("");
password.setText("");
repassword.setText("");
builder.hide();
}
});
}
else
if(regObj.getString("status").equals("USER_EXIST"))
{
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Login.this, "Username already exists. please enter a different Username", Toast.LENGTH_LONG).show();
}
});
}
else
{
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Login.this, " Registration UnSuccessfull", Toast.LENGTH_LONG).show();
}
});
}
}
catch(final Exception e)
{
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Login.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
}).start();
}
else
{
Toast.makeText(getApplicationContext(), "Password Do Not Match", Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Please Fill All Fields", Toast.LENGTH_LONG).show();
}
}
});
return builder;
}
return null;
}
public class GetState extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Fetching States...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
AccessNwData jsonParser = new AccessNwData();
String json = jsonParser.getState();
System.out.println("got "+json);
if(!json.equals("false"))
{
Log.e("log_tag", "> " + json);
if (json != null) {
try {
JSONArray jstate = new JSONArray(json);
for(int i=0;i<jstate.length();i++)
{
JSONObject stateObj = (JSONObject) jstate.getJSONObject(i);
stateList.add(stateObj.getString("name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Didn't receive any data from server!", Toast.LENGTH_LONG).show();
Log.e("log_tag", "Didn't receive any data from server!");
}
});
}
}else{
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Problem Fetching State", Toast.LENGTH_LONG).show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
spinnerAdapter.notifyDataSetChanged();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
以下是该类的代码:
12-01 11:54:26.121 19624-19624/global.get.mater/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: global.get.vancampaign, PID: 19624
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ArrayAdapter.notifyDataSetChanged()' on a null object reference
at global.get.vancampaign.Login$GetState.onPostExecute(Login.java:353)
at global.get.vancampaign.Login$GetState.onPostExecute(Login.java:294)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
logcat的:
Class Student
def self.get_students_which_has_not_scholarship
students = self.all
scholar_ship_students_ids = Scholarship.pluck(:student_id)
students.select{ |student| !scholar_ship_students_ids.include?(student.id) }
end
end
答案 0 :(得分:1)
下面:&#39;
if(!Internet.isConnectingToInternet())
{
}
else
{
asyncstate.execute(); //<< this line causing issue
}
因为在调用onPostExecute
方法的原因中,spinnerAdapter
ArrayAdapter
的{{1}}对象为null
,因为只有在调用onCreateDialog
方法时才初始化它。 / p>
因此,为了让它工作,在执行AsyncTask之前调用onCreateDialog
方法:
if(!Internet.isConnectingToInternet())
{
// your code here...
}
else
{
showDialog(0);
asyncstate.execute(); //<< this line causing issue
}
答案 1 :(得分:0)
错误显示spinnerAdapter
为空,这是因为您的spinnerAdapter
是在onCreateDialog
中创建的,但您从未调用它。请检查一下。