您好我试图在Android中运行简单的登录代码。但是我的代码得到了一个空指针异常。
04-21 13:05:58.585 19275-19275/com.example.dell.code E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.code/com.example.dell.code.LoginActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.dell.code.LoginActivity.onCreate(LoginActivity.java:42)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
你能帮帮我吗?
content_main.xml
<Button
android:id="@+id/btn_valider"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:background="@drawable/colorbutton"
android:padding="12dp"
android:text="@string/Connexion"/>
MainActivity.xml
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
......................................................
private Button btn_valider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
btn_valider = (Button) findViewById(R.id.btn_valider);
btn_valider.setOnClickListener(this);
}
@Override
public void onClick(View v){
if(v==btn_valider){
startActivity(new Intent(this, LoginActivity.class));
}
}
}
logcat说问题出在setOnClickListener上。我不知道它有什么问题。
LoginActivity.java
....................................
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
public static final String URL_LOG = "http://10.0.2.2/PHP/login.php";
public static final String KEY_login = "login";
public static final String KEY_password = "password";
private EditText et_login, et_password;
private Button btn_valider;
private String login;
private String password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
et_login = (EditText) findViewById(R.id.et_login);
et_password = (EditText) findViewById(R.id.et_password);
btn_valider = (Button) findViewById(R.id.btn_valider);
btn_valider.setOnClickListener(this);
}
private void userLogin(){
login = et_login.getText().toString().trim();
password = et_password.getText().toString().trim();
StringRequest stringRequest= new StringRequest(Request.Method.POST,URL_LOG,
new Response.Listener<String> () {
@Override
public void onResponse(String response) {
if (response.trim().equals("successs")) {
OpenProfile();
} else {
Toast.makeText(LoginActivity.this, response, Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
Toast.makeText(LoginActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams() throws AuthFailureError{
Map<String,String> map=new HashMap<String,String>();
map.put(KEY_login, login);
map.put(KEY_password,password);
return map;
}
};
RequestQueue requestqueue= Volley.newRequestQueue(this);
requestqueue.add(stringRequest);
}
private void OpenProfile(){
Intent intent = new Intent(LoginActivity.this,AccountNavigationActivity.class);
intent.putExtra(KEY_login,login);
startActivity(intent);
}
public void onClick(View v){userLogin();}
提前致谢