我有一个我正在处理的Android应用程序,它有一个导航抽屉。 我有抽屉内容和一个片段视图。 代码编译,但片段是视图为空(视图中没有文本显示)。
有人可以帮我弄清楚我做错了吗?
这是我的代码(来自片段的java类文件):
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