Iam嘗試設置從數據庫獲得的Edittext Widget的文本(數據),但是Iam正在獲取上述錯誤,蔭試圖解決,但沒有得到實際的想法,請幫助我,在此先感謝,嘗試在空對象引用上調用虛擬方法'void android.widget.EditText.setText(java.lang.CharSequence)'
這是我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)'
- at android.app.ActivityThread.performLaunchActivi TY(ActivityThread.java:2314)
- 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388)
- 在android.app.ActivityThread.access $ 800(ActivityThread.java:148)
- 在安卓.app.ActivityThread $ H.handleMessage(ActivityThread.java:1292)
- 在android.os.Handler.dispatchMessage(Handler.java:102)
- 在android.os.Looper.loop(Looper.java:135 )
- at android.app.ActivityThread.main(ActivityThread.java:5312) at 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.lang.NullPointerException:試圖調用虛擬方法'void android.widget.EditText.setText(java.lang.CharSequence)'on a空對象引用
- 在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)
- at 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)
- at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java :901) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
請幫我解決這個
'etName'爲空 – ThomasThiebaud
當您發佈堆棧跟蹤時,不要說等等。發佈整個事情。 – Sweeper
好的,謝謝 – Anil