gpt4 book ai didi

android - 开始一个使用 asynctask 的新 Activity

转载 作者:行者123 更新时间:2023-11-30 02:19:34 27 4
gpt4 key购买 nike

当我尝试启动将使用 AsyncTask 的新 Activity 时,我的应用程序崩溃了,我的 LoginActivity。当我开始任何其他不使用 AsyncTask 的 Activity 时,它工作正常。这是我的 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");
}
}
}
}

最佳答案

如日志中所示:

NullPointerException: Attempt to invoke virtual method 'int android.widget.Button.getId()' on a null object reference

因为 main_login Button 对象是 null

在调用 getId 方法之前初始化 main_login 按钮对象:

main_login = (Button)findViewById(R.id.mainLogin);
main_login.setOnClickListener(this);

关于android - 开始一个使用 asynctask 的新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28802093/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com