尝试在空对象引用

时间:2015-10-27 13:49:53

标签: java android android-activity runtimeexception

我试图设置从数据库中获取的Edittext小部件的文本(数据),但是我得到了上述错误,Iam试图解决但没有得到实际的想法,请帮我这个,提前谢谢,

这是我的MainActivity.java

package com.mycompany.newlogin;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

Button btLogout;
EditText etName,etPhone,etEmail,etUsername;
private static String url = "jdbc:mysql://31.170.160.74:3306/a5582611_mane";
private static String user = "a5582611_nanu";
private static String password = "sonne0";
UserLocalStore userLocalStore;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    etName=(EditText)findViewById(R.id.etName);
    etPhone=(EditText)findViewById(R.id.etPhone);
    etEmail=(EditText)findViewById(R.id.etEmail);
    etUsername=(EditText)findViewById(R.id.etUsername);
    btLogout=(Button)findViewById(R.id.btLogout);
    userLocalStore=new UserLocalStore(this);

    btLogout.setOnClickListener(this);

}
@Override
protected void onStart(){
    super.onStart();
    if(authenticate()==true){
        //go to your question window
        displayUserDetails();
    }else {
        startActivity(new Intent(MainActivity.this,Login.class));

    }
}
private void displayUserDetails(){
    User user=userLocalStore.getLoggedInUser();
    etName.setText(user.name);
    etEmail.setText(user.email);
    etUsername.setText(user.username);
}

private boolean authenticate(){
    return userLocalStore.getUserLoggedIn();
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btLogout:
            userLocalStore.clearUserData();
            userLocalStore.setUserLoggedIn(false);
           startActivity(new Intent(this, Login.class));
            break;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

我的activity_xml文件是这样的,     

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/etName"
    android:layout_marginBottom="10dp"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Email"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/Email"
    android:layout_marginBottom="10dp"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Username"/>
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/etUsername"
    android:layout_marginBottom="10dp"
    />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btLogout"
    android:text="Logout"
    android:layout_gravity="center"/>

这是我的错误详情,

  • 致命的例外:主要 处理:com.mycompany.newlogin,PID:2787
  • java.lang.RuntimeException:无法启动活动ComponentInfo {com.mycompany.newlogin / com.mycompany.newlogin.MainActivity}:java.lang.NullPointerException:尝试调用虚方法'void android.widget.EditText.setText (java.lang.CharSequence)'对空对象引用
  • 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2314)
  • 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
  • 在android.app.ActivityThread.access $ 800(ActivityThread.java:148)
  • 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1292)
  • 在android.os.Handler.dispatchMessage(Handler.java:102)
  • 在android.os.Looper.loop(Looper.java:135)
  • 在android.app.ActivityThread.main(ActivityThread.java:5312)         at java.lang.reflect.Method.invoke(Native Method)         在java.lang.reflect.Method.invoke(Method.java:372)
  • at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:901)<< li>
  • at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
  • 引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.EditText.setText(java.lang.CharSequence)'
  • at com.mycompany.newlogin.MainActivity.displayUserDetails(MainActivity.java:51)
  • at com.mycompany.newlogin.MainActivity.onStart(MainActivity.java:42)
  • 在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1243)
  • 在android.app.Activity.performStart(Activity.java:5969)
  • at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2277)<< li>
  • 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
  • 在android.app.ActivityThread.access $ 800(ActivityThread.java:148)
  • 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1292)
  • 在android.os.Handler.dispatchMessage(Handler.java:102)
  • 在android.os.Looper.loop(Looper.java:135)
  • 在android.app.ActivityThread.main(ActivityThread.java:5312)
  • at java.lang.reflect.Method.invoke(Native Method)
  • at java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:901) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

请帮我解决此问题

1 个答案:

答案 0 :(得分:1)

您的etEmail编辑文本为空:

代替 - etEmail=(EditText)findViewById(R.id.etEmail);

应该是 - etEmail=(EditText)findViewById(R.id.Email);

由于您的etEmail ID是“电子邮件”而不是“etEmail”。