【发布时间】:2016-01-26 23:20:18
【问题描述】:
我正在尝试设置从数据库获取的 Edittext 小部件的文本(数据),但我收到上述错误,我正在尝试解决但没有得到实际的想法,请帮助我,提前致谢,
这是我的 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) 在 java.lang.reflect.Method.invoke(本机方法) 在 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)
- 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.EditText.setText(java.lang.CharSequence)”
- 在 com.mycompany.newlogin.MainActivity.displayUserDetails(MainActivity.java:51)
- 在 com.mycompany.newlogin.MainActivity.onStart(MainActivity.java:42)
- 在 android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1243)
- 在 android.app.Activity.performStart(Activity.java:5969)
- 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2277)
- 在 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)
- 在 java.lang.reflect.Method.invoke(Native Method)
- 在 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)
请帮我解决这个问题
【问题讨论】:
-
当你发布堆栈跟踪时,不要说等等。发布整个事情。
标签: java android android-activity runtimeexception