我目前正在按照教程实现一个搜索栏来过滤我的listView,我的课程是这样的:
package com.example.fragmenttabhostdemo;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class Tab1 extends Fragment {
private ListView lv;
private EditText et;
private String listview_array[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE",
"SIX", "SEVEN", "EIGHT", "NINE", "TEN" };
private ArrayList<String> array_sort= new ArrayList<String>();
int textlength=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//lv = (ListView) getView().findViewById(R.id.ListView01);
//et = (EditText) getView().findViewById(R.id.EditText01);
//lv.setAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, listview_array));
}
问题是,一旦我删除了最后一行中的coments(“//”),这些应用程序就不起作用......它说:
06-11 12:06:37.964: D/AndroidRuntime(3101): Shutting down VM
06-11 12:06:37.964: W/dalvikvm(3101): threadid=1: thread exiting with uncaught exception (group=0xb1a17ba8)
06-11 12:06:37.984: E/AndroidRuntime(3101): FATAL EXCEPTION: main
06-11 12:06:37.984: E/AndroidRuntime(3101): Process: com.example.fragmenttabhostdemo, PID: 3101
06-11 12:06:37.984: E/AndroidRuntime(3101): java.lang.NullPointerException
06-11 12:06:37.984: E/AndroidRuntime(3101): at com.example.fragmenttabhostdemo.Tab1.onCreateView(Tab1.java:34)
我只是觉得使用这些变量是一个问题,但是,这个......或者将私密性从公用变为公用,都没有用。
一周前我开始学习android java,所以我真的很棒。
TY
P.S。返回缺失,因为评论下面有很多代码,但我省略了它只是为了保持简单。 (对不起)
答案 0 :(得分:3)
检查此方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
}
此方法的返回类型为View。在这里你应该膨胀适当的xml布局。所以改成它:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment,container,false);
//this is how you use findviewbyid inside fragment
Button myBut = (Button) view.findViewById(R.id.buttonMyBut);
return view;
}
答案 1 :(得分:3)
问题是您实际上从未真正为Fragment
设置视图。您无法在getView()
方法中调用onCreateView()
。相反,您应该执行以下操作。
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// This is where you actually get your view.
// Replace R.layout.fragment_layout with the layout file you are using
View myView = inflater.inflate(R.layout.fragment_layout, container, false);
// Now you can call findViewById on your newly inflated view
lv = (ListView) myView.findViewById(R.id.ListView01);
et = (EditText) myView.findViewById(R.id.EditText01);
lv.setAdapter(new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_list_item_1, listview_array));
// Be sure to return the view!
return myView;
}