当我尝试启动将使用Activity
AsyncTask
的新LoginActivity
时,我的应用会崩溃。当我开始我的其他任何不使用AsyncTask
的活动时,它工作正常。这是我的MainActivity
:
public class MainActivity extends ActionBarActivity implements OnClickListener {
private Button main_redeem;
private Button main_check;
private Button main_nearby;
private Button main_login;
private Button main_scan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main_redeem = (Button)findViewById(R.id.mainRedeem);
main_redeem.setOnClickListener(this);
main_check = (Button)findViewById(R.id.mainCheckPunches);
main_check.setOnClickListener(this);
main_nearby = (Button)findViewById(R.id.mainNearbyBusiness);
main_nearby.setOnClickListener(this);
main_scan = (Button)findViewById(R.id.mainLogin);
main_scan.setOnClickListener(this);
main_scan = (Button)findViewById(R.id.mainScan);
main_scan.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
// responds to button clicks
if(v.getId() == main_redeem.getId()){
Intent redeem_intent = new Intent(MainActivity.this, RedeemPunches.class);
startActivity(redeem_intent);
}
else if(v.getId() == main_check.getId()){
Intent check_intent = new Intent(MainActivity.this, CheckPunches.class);
startActivity(check_intent);
}
else if(v.getId() == main_nearby.getId()){
Intent nearby_intent = new Intent(MainActivity.this, NearbyBusinesses.class);
startActivity(nearby_intent);
}
//This is the activity that crashes
else if(v.getId() == main_login.getId()){
Intent login_intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(login_intent);
}
else if(v.getId() == main_scan.getId()){
Intent scan_intent = new Intent(MainActivity.this, ScanCode.class);
startActivity(scan_intent);
}
}
}
03-01 18:44:24.841: E/AndroidRuntime(1278): FATAL EXCEPTION: main
03-01 18:44:24.841: E/AndroidRuntime(1278): Process: com.nb.punchcard, PID: 1278
03-01 18:44:24.841: E/AndroidRuntime(1278): java.lang.NullPointerException: Attempt to invoke virtual method 'int android.widget.Button.getId()' on a null object reference
03-01 18:44:24.841: E/AndroidRuntime(1278): at com.nb.punchcard.MainActivity.onClick(MainActivity.java:94)
03-01 18:44:24.841: E/AndroidRuntime(1278): at android.view.View.performClick(View.java:4756)
03-01 18:44:24.841: E/AndroidRuntime(1278): at android.view.View$PerformClick.run(View.java:19749)
03-01 18:44:24.841: E/AndroidRuntime(1278): at android.os.Handler.handleCallback(Handler.java:739)
03-01 18:44:24.841: E/AndroidRuntime(1278): at android.os.Handler.dispatchMessage(Handler.java:95)
03-01 18:44:24.841: E/AndroidRuntime(1278): at android.os.Looper.loop(Looper.java:135)
03-01 18:44:24.841: E/AndroidRuntime(1278): at android.app.ActivityThread.main(ActivityThread.java:5221)
03-01 18:44:24.841: E/AndroidRuntime(1278): at java.lang.reflect.Method.invoke(Native Method)
03-01 18:44:24.841: E/AndroidRuntime(1278): at java.lang.reflect.Method.invoke(Method.java:372)
03-01 18:44:24.841: E/AndroidRuntime(1278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-01 18:44:24.841: E/AndroidRuntime(1278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
以下是我的LoginActivity的代码:
public class LoginActivity extends Activity {
private EditText txtName;
private EditText txtPass;
private Button btnSubmit;
TextView strError;
TextView txtView;
String serverResponse = null;
private static final String TAG_STATUS = "strStatus";
JSONObject user = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
txtName = (EditText) findViewById(R.id.login_name_entry);
txtPass = (EditText) findViewById(R.id.login_pass_entry);
btnSubmit = (Button) findViewById(R.id.login_submit_btn);
strError = (TextView) findViewById(R.id.login_textView_error);
txtView = (TextView) findViewById(R.id.textView1);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String strName = txtName.getText().toString();
String strPassword = txtPass.getText().toString();
validateVendor task = new validateVendor();
task.execute(new String[]{strName, strPassword});
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class validateVendor extends AsyncTask<String, String, JSONObject>{
@Override
protected JSONObject doInBackground(String... params) {
ArrayList<NameValuePair> postParam = new ArrayList<NameValuePair>();
postParam.add(new BasicNameValuePair("strName", params[0]));
postParam.add(new BasicNameValuePair("strPassword", params[1]));
//JSONObject serverRes = null;
JSONParser jParser = new JSONParser();
JSONObject serverRes = null;
try {
serverRes = jParser.getJSONFromUrl(//myURL);
//serverRes =
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return serverRes;
}
@Override
protected void onPostExecute(JSONObject serverRes){
String strRes = "";
try {
strRes = serverRes.getString(TAG_STATUS);
txtView.setText(strRes);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(strRes.equals("accepted")){
strError.setText("Correct");
Intent i = new Intent(LoginActivity.this,MainActivity.class);
startActivity(i);
}
else{
strError.setText("Sorry!! Incorrect Username or Password");
}
}
}
}
答案 0 :(得分:1)
与日志一样:
NullPointerException:尝试调用虚方法'int 关于空对象引用的android.widget.Button.getId()'
因为main_login
Button对象为null
。
在调用main_login
方法之前初始化getId
Button对象:
main_login = (Button)findViewById(R.id.mainLogin);
main_login.setOnClickListener(this);