TextView.setText()抛出空指针异常

时间:2013-12-13 16:14:47

标签: java android android-layout user-interface textview

你好,很高兴见到你们。我有一个问题一直困扰着我2天。让我谈谈这一点。

我有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 Exception。 我搜索了整个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();
    }





}

2 个答案:

答案 0 :(得分: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 :(得分:0)

我相信你需要:

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

适用于Facebook,电子邮件,移动和其他信息。

相关问题