你好,很高興認識你們。我有一個令我困擾的問題連續兩天。讓我明白這一點。TextView.setText()拋出空指針異常
我有2個活動,第一個填充靜態Hashtable和在第二個我想(第二活動的onCreate方法內側和的setContentView()之後)
TextView name=(TextView)findViewById(R.id.ForeignUserName);
,然後在在的onResume()
name.setText("ASDFASD");
Personal_Message.setText("sdfgsfgsf");
Facebook.setText("adfads");
Email.setText("adsfads");
Mobile.setText("adsfadsf");
AdditionalInformation.setText("sdfads");
(以下簡稱 「asdfasd」 字符串只是爲了調試我會Hashtable.get(I)後)
第一TextView的名字被填充,但其他Ø nes拋出空指針異常。 我搜索了整個StackOverflow。我相信我錯過了很簡單的事情。任何幫助將非常感激。
謝謝!
XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ForeignProfileActivityLayout"
>
<ImageView
android:id="@+id/ForeignImageView01"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/Profile_Image"
android:cropToPadding="true"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:minHeight="100dp"
android:minWidth="100dp"
android:src="@drawable/person" />
<TextView
android:id="@+id/ForeignPersonalMessage"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_below="@+id/ForeignUserName"
android:layout_alignParentRight="true"
android:ems="10"
android:hint="@string/Personal_Message"
/>
<TextView
android:id="@+id/ForeignUserEmail"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_below="@+id/ForeignFacebookPage"
android:ems="10"
android:hint="@string/email"
/>
<TextView
android:id="@+id/ForeignMobileNumber"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_below="@+id/ForeignUserEmail"
android:ems="10"
android:hint="@string/Your_mobile_phone_number"
/>
<TextView
android:id="@+id/ForeignUserInfo"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_below="@+id/ForeignMobileNumber"
android:ems="10"
android:height="100dp"
android:hint="@string/Additional_Information"
/>
<TextView
android:id="@+id/ForeignUserName"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:ems="10"
android:hint="@string/Your_Name" />
<TextView
android:id="@+id/ForeignFacebookPage"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/ForeignPersonalMessage"
android:layout_marginTop="86dp"
android:ems="10"
android:hint="@string/FBPage" />
</RelativeLayout>
ForeignProfile.java
public class ForeignProfile extends Activity {
TextView name,Facebook,Email,AdditionalInformation,Mobile,Personal_Message;
AdView adView;
static final String MYD_AD_UNIT_ID="-----------";
static final String TAG="ForeignProfile";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BugSenseHandler.initAndStartSession(this, "-----");
setContentView(R.layout.activity_foreign_profile);
Personal_Message=new TextView(getApplicationContext());
adView=new AdView(this, AdSize.BANNER, MYD_AD_UNIT_ID);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.ForeignProfileActivityLayout);
RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT); // check this out
layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, RelativeLayout.TRUE);
adView.setGravity(Gravity.BOTTOM);
layout.addView(adView,layoutParams);
AdRequest req=new AdRequest();
req.addTestDevice("----------");
adView.loadAd(req);
Log.d(TAG, "Foreign Profile Created");
name=(TextView)findViewById(R.id.ForeignUserName);
Personal_Message=(TextView)findViewById(R.id.personalMessage);
Facebook=(TextView)findViewById(R.id.ForeignFacebookPage);
Email=(TextView)findViewById(R.id.ForeignUserEmail);
Mobile=(TextView)findViewById(R.id.ForeignMobileNumber);
AdditionalInformation=(TextView)findViewById(R.id.ForeignUserInfo);
}
@Override
public void onDestroy() {
adView.destroy();
super.onDestroy();
}
@Override
protected void onResume() {
name.setText("ASDFASD");
Personal_Message.setText("sdfgsfgsf");
Facebook.setText("adfads");
Email.setText("adsfads");
Mobile.setText("adsfadsf");
AdditionalInformation.setText("sdfads");
super.onResume();
}
}
這意味着TextView的初始化失敗給NPE – Raghunandan
發表您的XML wher你有textviews並在您初始化textviews – Raghunandan
如姓名TextView的代碼你通過findViewById引用你的其他的TextView的(Personal_Message,臉譜,...)? – saiful103a