Привет и приятно познакомиться с вами, ребята. У меня проблема, которая беспокоит меня в течение 2 дней подряд. Позвольте мне понять.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 заполнена, а другая о исключение Null Pointer. Я искал весь 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 у вас есть порог TextViews и код, где вы инициализировать ваши TextViews – Raghunandan
как имя TextView вы ссылку вашего другого TextView в (Personal_Message, Facebook, ...) с помощью findViewById? – saiful103a