Android 导航抽屉应用的片段视图中未显示文本
Text not appearing in Fragment view in Android Navigation drawer app
我有一个正在开发的 android 应用程序,它有一个导航抽屉。
我有抽屉内容和一个开始的片段视图。
代码编译,但片段是空视图(视图中没有显示文本)。
谁能帮我弄清楚我做错了什么?
这是我的代码(来自片段的 java class 文件):
package com.example.ironmantis7x.infonewmuslim;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
*
*/
public class TawheedFragment extends Fragment {
public TawheedFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tawheed, container, false);
TextView text = (TextView) rootView.findViewById(R.id.txttawheed);
//text.setText("your text!");
InputStream is = getResources().openRawResource(R.raw.tawheed);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
String entireFile = "";
try {
while((line = br.readLine()) != null) { // <--------- place readLine() inside loop
entireFile += (line + "\n"); // <---------- add each line to entireFile
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//TextView text = null;
//text.setText(entireFile); // <------- assign entireFile to TextView
//assert text != null;
if (text != null) {
text.setText(entireFile);
}
return rootView;
}
}
这是片段xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/txttawheed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:gravity="fill_horizontal|top|left"
android:scrollbars = "vertical"/>
</ScrollView>
</LinearLayout>
谢谢。
ironmantis7x
我没有正确地将片段链接到主要 activity。我修好了它。
感谢大家的帮助,让我 post!
ironmantis7x
我有一个正在开发的 android 应用程序,它有一个导航抽屉。 我有抽屉内容和一个开始的片段视图。 代码编译,但片段是空视图(视图中没有显示文本)。
谁能帮我弄清楚我做错了什么?
这是我的代码(来自片段的 java class 文件):
package com.example.ironmantis7x.infonewmuslim;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
*
*/
public class TawheedFragment extends Fragment {
public TawheedFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tawheed, container, false);
TextView text = (TextView) rootView.findViewById(R.id.txttawheed);
//text.setText("your text!");
InputStream is = getResources().openRawResource(R.raw.tawheed);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
String entireFile = "";
try {
while((line = br.readLine()) != null) { // <--------- place readLine() inside loop
entireFile += (line + "\n"); // <---------- add each line to entireFile
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//TextView text = null;
//text.setText(entireFile); // <------- assign entireFile to TextView
//assert text != null;
if (text != null) {
text.setText(entireFile);
}
return rootView;
}
}
这是片段xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/txttawheed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16dp"
android:gravity="fill_horizontal|top|left"
android:scrollbars = "vertical"/>
</ScrollView>
</LinearLayout>
谢谢。
ironmantis7x
我没有正确地将片段链接到主要 activity。我修好了它。 感谢大家的帮助,让我 post!
ironmantis7x