2013-12-13 184 views
-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的名字被填充,但其他Ø 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(); 
    } 





} 
+1

这意味着TextView的初始化失败给NPE – Raghunandan

+0

发表您的XML wher你有textviews并在您初始化textviews – Raghunandan

+0

如姓名TextView的代码你通过findViewById引用你的其他的TextView的(Personal_Message,脸谱,...)? – saiful103a

回答

2

你有以下的XML

<TextView 
     android:id="@+id/ForeignPersonalMessage" // id is ForeignPersonalMessage 

更改以下

Personal_Message=(TextView)findViewById(R.id.personalMessage); 

Personal_Message=(TextView)findViewById(R.id.ForeignPersonalMessage); 
+1

就是这样。谢谢Raghunandan!我不能相信我错过了这个!再次感谢! –

0

我相信你需要:

TextView facebook=(TextView)findViewById(R.id.ForeignFacebookPage); 
facebook.setText("adfads"); 

对于Facebook,电子邮件,移动和AdditionalInformation。

+0

没有它的初始化正确,因为我可以在发布 – Raghunandan

+0

@Raghunandan的问题中看到,是的。 – lindenrovio