gpt4 book ai didi

java - onclick 监听器的 Android 空指针异常

转载 作者:行者123 更新时间:2023-12-01 12:06:24 25 4
gpt4 key购买 nike

以下代码给了我空指针异常,即使我找不到代码的任何问题。尝试将其注释掉,它似乎有效。但还是没能找出代码有什么问题?尝试了我自己的注释代码等问题,现在下面的代码给了我空指针异常

日志猫

12-19 05:49:14.833: E/AndroidRuntime(3392): Caused by:java.lang.NullPointerException: Attempt to invoke virtual method 'voidandroid.widget.Button.setOnClickListener(android.view.View$OnClickListener)'on a null object reference 12-19 05:49:14.833: E/AndroidRuntime(3392):atcom.techiequickie.bharath.boadraf.newBet_activity.onCreate(newBet_activity.java:80)

代码:

package com.techiequickie.bhr.boadraf;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import databasehandler.DatabaseHandler;
import databasehandler.UserFunctions;

/**
* Created by YP on 17-Nov-14.
*/
public class newBet_activity extends ActionBarActivity
{
EditText inputBetname, inputBetbrief, inputBetdescription;
SeekBar valueSeekbar; //= null;
Button placeBet;


// JSON Response node names
private static String KEY_SUCCESS = "success";
@SuppressWarnings("unused")
private static String KEY_ERROR = "error";
@SuppressWarnings("unused")
private static String KEY_ERROR_MSG = "error_msg";
//private static String KEY_UID = "uid";
private static String KEY_BETNAME = "bet_name";
private static String KEY_BETBRIEF = "bet_brief";
private static String KEY_BETDESCRIPTION = "bet_description";
private static String KEY_BETVALUE = "bet_value";
// private static String KEY_CREATED_AT = "created_at";


@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.newbet);
// Importing all assets like buttons, text fields
inputBetname = (EditText) findViewById(R.id.betName_et);
inputBetbrief = (EditText) findViewById(R.id.betBriefDescription_et);
inputBetdescription = (EditText) findViewById(R.id.betDescription_et);
placeBet = (Button) findViewById(R.id.btn_newbet);

//valueSeekbar = (SeekBar) findViewById(R.id.betValue_bar);

/*
valueSeekbar.setOnSeekBarChangeListener
(

new SeekBar.OnSeekBarChangeListener()
{
int progressChanged = 0;

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progressChanged = progress;
}

public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}

public void onStopTrackingTouch(SeekBar seekBar) {
Toast.makeText(newBet_activity.this, "seek bar progress:" + progressChanged, Toast.LENGTH_SHORT).show();
}
}
);*/


placeBet.setOnClickListener(new View.OnClickListener()
{


public void onClick(View view)
{
String betname = inputBetname.getText().toString();
String betBrief = inputBetbrief.getText().toString();
String betDescription = inputBetdescription.getText().toString();
//StringBuilder betValue = ((toString()) valueSeekbar.getProgress());

//new BetRegisterTask().execute(betname,betBrief,betDescription);
}
}
);


}

/*
class BetRegisterTask extends AsyncTask<String, Void, String>
{

@Override
protected String doInBackground(String... params)
{

UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.createBet(params[0], params[1], params[2]);

// check for login response
try
{
if (json.getString(KEY_SUCCESS) != null)
{

String res = json.getString(KEY_SUCCESS);
if (Integer.parseInt(res) == 1)
{
// user successfully registred
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.optJSONObject("user");

// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.newBet(json_user.getString(KEY_BETNAME), json_user.getString(KEY_BETBRIEF), json.getString(KEY_BETDESCRIPTION));
return "1";

}

}
} catch (JSONException e)
{
e.printStackTrace();
}

return "0";
}

@Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
if (s.equals("1"))
{

// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), Loginactivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Registration Screen
finish();
showToastforSucessfulBetCreation();

}
else
{
// Error in registration
//registerErrorMsg.setText("Error occurred in registration");
showToastforUnsucessfulBetCreation();
}
}
}*/

public void showToastforSucessfulBetCreation() {
Toast toast = Toast.makeText(this, "Registration Sucessfull", Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 30);
toast.show();

}


public void showToastforUnsucessfulBetCreation() {
Toast toast = Toast.makeText(this, "Registration UnSucessfull", Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM, 0, 30);
toast.show();

}

}

最佳答案

placeBet 尚未初始化,这就是它给出空指针异常的原因

检查以下行

 palceBet=findViewId(R.id.btn_newbet);

关于java - onclick 监听器的 Android 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27560449/

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