我对代码的错误感到迷茫。它不会显示页面,我确定错误是在Java代码中的某个地方。因为我检查布局和它的工作完全没有,直到我把后端。但我无法弄清楚错误在哪里。所以请有人帮助我。
package calculator.apk;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class signMeup extends ActionBarActivity {
Button sign;
EditText name,birthD,birthM,birthY,pass;
String fname,passWord,bdate,bmonth,byear,gender;
CheckBox checkgen;
RadioButton female, male;
RadioGroup group;
int inputField;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
name = (EditText) findViewById (R.id.editText1);
birthD = (EditText) findViewById (R.id.editText2);
birthM = (EditText) findViewById (R.id.editText3);
birthY = (EditText) findViewById (R.id.editText4);
pass = (EditText) findViewById (R.id.editText5);
male =(RadioButton) findViewById (R.id.radioButton1);
female =(RadioButton) findViewById (R.id.radioButton2);
group = (RadioGroup)findViewById(R.id.radioGroup);
sign.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try{
fname = name.getText().toString();
checkInput (fname);
bdate = birthD.getText().toString();
checkInput(bdate);
bmonth = birthM.getText().toString();
checkInput(bmonth);
byear = birthY.getText().toString();
checkInput(byear);
passWord = pass.getText().toString();
checkInput(passWord);
checkRadioButton();
Intent intent = new Intent(getBaseContext(), profile.class);
intent.putExtra("fName", fname);
intent.putExtra("bDate", bdate);
intent.putExtra("bMonth", bmonth);
intent.putExtra("bYear", byear);
intent.putExtra("pWord", passWord);
intent.putExtra("genDer", gender);
startActivity(intent);
}
catch (Exception e) {
// do nothing
}
}
});
}
public void checkRadioButton()
{
int index = group.getCheckedRadioButtonId();
if(index==-1){
inputField++;
}
else{
//Checking for Male
if(male.isChecked()){
gender="Male";
Toast.makeText(getApplicationContext(), "You selected Male.", Toast.LENGTH_SHORT).show();
}
//Checking for Female
else if(female.isChecked()){
gender="Female";
Toast.makeText(getApplicationContext(), "You selected Female.", Toast.LENGTH_SHORT).show();
}
}
}
public void checkInput(String input)
{
if(input.matches("")){
inputField++;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calcu, 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);
}
}
答案 0 :(得分:1)
在以下行中,您将获得NULL POINTER EXCEPTION
sign.setOnClickListener(new View.OnClickListener() {
因为您永远不会像其他属性一样初始化Button sign
。
sign = (Button)findViewById(R.id.yourButtonId);