如果我从Java代码中访问textview它可以工作。所以我假设我在某处弄错了。有谁能看到这个问题?
package com.projectcaruso.naturalfamilyplaning;
import com.projectcaruso.naturalfamilyplanning.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class HistoryFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SQLHelper entry = new SQLHelper(getActivity());
TextView tv = (TextView) getView().findViewById(R.id.tvSQLinfo);
entry.open();
String results = entry.getData();
entry.close();
tv.setText(results);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_history, container, false);
}
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation ="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="date" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="temp" />
</TableRow>
</TableLayout>
<TextView
android:id="@+id/tvSQLinfo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Get info from db" />
</LinearLayout>
logcat的:
05-12 22:55:01.799: E/AndroidRuntime(1983): FATAL EXCEPTION: main
05-12 22:55:01.799: E/AndroidRuntime(1983): java.lang.NullPointerException
05-12 22:55:01.799: E/AndroidRuntime(1983): at com.projectcaruso.naturalfamilyplaning.HistoryFragment.onCreate(HistoryFragment.java:20)
05-12 22:55:01.799: E/AndroidRuntime(1983): at android.support.v4.app.Fragment.performCreate(Fragment.java:1437)
05-12 22:55:01.799: E/AndroidRuntime(1983): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
05-12 22:55:01.799: E/AndroidRuntime(1983): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-12 22:55:01.799: E/AndroidRuntime(1983): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-12 22:55:01.799: E/AndroidRuntime(1983): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-12 22:55:01.799: E/AndroidRuntime(1983): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
05-12 22:55:01.799: E/AndroidRuntime(1983): at android.os.Handler.handleCallback(Handler.java:725)
05-12 22:55:01.799: E/AndroidRuntime(1983): at android.os.Handler.dispatchMessage(Handler.java:92)
05-12 22:55:01.799: E/AndroidRuntime(1983): at android.os.Looper.loop(Looper.java:137)
05-12 22:55:01.799: E/AndroidRuntime(1983): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-12 22:55:01.799: E/AndroidRuntime(1983): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 22:55:01.799: E/AndroidRuntime(1983): at java.lang.reflect.Method.invoke(Method.java:511)
05-12 22:55:01.799: E/AndroidRuntime(1983): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-12 22:55:01.799: E/AndroidRuntime(1983): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-12 22:55:01.799: E/AndroidRuntime(1983): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
问题是,您对getView()
的调用会返回null
。
为什么?
因为在onCreate()
之前调用了onCreateView()
。
有关详细信息,请查看Fragment生命周期:http://developer.android.com/guide/components/fragments.html#Creating
<强>解决方案:强>
将您的逻辑也移到onCreateView()
方法中,并继续引用膨胀的视图,而不是通过getView()
调用它。
它看起来像这样:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);
SQLHelper entry = new SQLHelper(getActivity());
entry.open();
String results = entry.getData();
entry.close();
TextView tv = (TextView) view.findViewById(R.id.tvSQLinfo);
tv.setText(results);
return view;
}