android textview中没有显示文字?

时间:2015-01-01 11:38:23

标签: android user-interface

我有一个简单的布局和必须更新textview的java类。无论出于何种原因,文本都没有显示。我检查了变量,它肯定有值显示。只有文本视图未在UI中更新。

public class PlayerActivity extends YouTubeBaseActivity implements
    YouTubePlayer.OnInitializedListener, AdListener{

    // create string variables
    String YOUTUBE_APIKEY;
    String ID, Title;
    private TextView titleview;

    private InterstitialAd interstitial;
    private static final String LOG_TAG = "LayarTancep";

    // create object of view
    YouTubePlayerView youTubePlayerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_player);


        // connect view object and view id on xml
        youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);

        titleview = (TextView) findViewById(R.id.textView1);

        // create the interstitial
        interstitial = new InterstitialAd(this, getString(R.string.admob_id));

        // load interstitialAd
        Ads.loadInterstitialAd(interstitial);

        // set Ad Listener to use the callbacks below
        interstitial.setAdListener(this);

        // get YOUTUBE APIKEY
        YOUTUBE_APIKEY = getString(R.string.youtube_apikey);

        // get video id from previous page
        Intent i = getIntent();
        ID = i.getStringExtra("id");
        Title = i.getStringExtra("title");


        titleview.setText(Title);

        Log.i("TESTING", Title);

    }
}

这是布局文件。

<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:orientation="vertical"
    tools:context=".MainActivity"
    android:background="@android:color/white" >



        <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="56dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />




    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/youtubeplayerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="142dp" >
    </com.google.android.youtube.player.YouTubePlayerView>


</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

    ...
    //titleview = new TextView(PlayerActivity.this); remove this line 
    titleview = (TextView) findViewById(R.id.textView1);
    ...

当您引用已在布局文件中定义的TextView对象时,您不必创建新的TextView对象。

答案 1 :(得分:0)

删除titleview = new TextView(PlayerActivity.this);它会完美地运作