我遇到了嘗試在我的android應用程序上將文本設置爲TextView的實際問題。我試圖顯示的字符串通過意圖傳遞給另一個活動。我似乎已經成功地從意圖中獲取了額外的內容,但是我無法將其附加到我的TextView。請有人可以幫忙嗎?我在整個使用碎片。無法在Android中將TextText設置爲TextView
我所得到的是我的佈局顯示沒有我想要分配的文本。
Fragment:2
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.article_fragment, parent, false);
journalTitle = (String) getArguments().getString(EXTRA_JOURNALTITLE); // extra from intent
Log.d("onCreateView", "a.getJournalTitle: " + journalTitle); // this works. displays in logCat
journalTitleView = (TextView) v
.findViewById(R.id.journalTitle_articleFragment_textView);
journalTitleView.setText(journalTitle);
佈局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/journalTitle_articleFragment_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="journal title"
android:textSize="12sp" />
<TextView
android:id="@+id/pubYear_articleFragment_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/journalTitle_articleFragment_textView"
android:text="pubYear"
android:textSize="12sp" />
<TextView
android:id="@+id/volume_articleFragment_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/pubYear_articleFragment_textView"
android:text="volume"
android:textSize="12sp" />
<TextView
android:id="@+id/issue_articleFragment_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/volume_articleFragment_textView"
android:text="issue"
android:textSize="12sp" />
<TextView
android:id="@+id/pageInfo_articleFragment_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/issue_articleFragment_textView"
android:text="pageInfo"
android:textSize="12sp" />
<TextView
android:id="@+id/title_articleFragment_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/journalTitle_articleFragment_textView"
android:gravity="center"
android:layout_marginTop="25dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="article title"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/authorString_articleFragment_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title_articleFragment_textView"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="authors"
android:textSize="12sp" />
<TextView
android:id="@+id/abstractText_articleFragment_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/authorString_articleFragment_textView"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="abstractText"
android:textSize="15sp" />
你可以發佈佈局嗎? – Blackbelt 2014-11-05 16:33:47
嘗試一些像。不是soln,而是..在xml中設置文本屬性n代碼中的getText,並在logcat和Ui中查看。然後追加一些字符串並再次檢查。 – Manmohan 2014-11-05 16:38:50
您是否收到異常?你的article_fragment.xml是否有一個帶有journalTitle_articleFragment_textView的TextView? – 2014-11-05 16:41:20