从android studio

时间:2016-10-05 07:26:11

标签: android android-fragments

我尝试在mainactivity.java的myfragment.xml中将文本设置为TextView 但发生以下错误。

这是我的main_activity.java

@Override                                                                                   
public boolean onQueryTextChange(String newText) {                                          

    if(aBoolean){                                                                           
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative);       
        relativeLayout.setVisibility(View.INVISIBLE);                                       
        FragmentManager fragmentManager = getSupportFragmentManager();                      
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();       
        fragmentTransaction.setCustomAnimations(R.anim.top_to_bottom, R.anim.bottom_to_top);
        dropdown dropdown = new dropdown();                                                 
        fragmentTransaction.add(R.id.fragment_container,dropdown);                          
        fragmentTransaction.commit();                                                       
        dropdown.setTextView("Heading1","HelloWorld");                                      
        aBoolean=false;                                                                     

    }                                                                                       

    else {                                                                                  
        FragmentManager fragmentManager = getSupportFragmentManager();                      
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();       
        dropdown dropdown = new dropdown();                                                 

        fragmentTransaction.add(R.id.fragment_container,dropdown);                          
        fragmentTransaction.commit();                                                       
        dropdown.setTextView("Heading1","HelloWorld");                                      
    }      

我在片段文件

中为settext编写了一个方法

dropdown.java

public class dropdown extends Fragment {
    View main_view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v =  inflater.inflate(R.layout.fragment_dropdown, container, false);
        main_view = v;
        TextView textView = (TextView) v.findViewById(R.id.heading1);
        textView.setText("hello");
        return v;
    }

    public void setTextView(String id, String text){
        TextView textView = (TextView) main_view.findViewById(R.id.heading1);
        textView.setText("HelloWorld");
    }

}

运行时发生以下错误

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.app.Fragment.getId()' on a null object reference 

此错误显示在dropdown.setTextView(" Heading1"," HelloWorld");

的行中

3 个答案:

答案 0 :(得分:0)

代码不够。但让我猜问题.. 我认为你的片段在android.app包中,而你是android.support.v4.app.Fragment

答案 1 :(得分:0)

如果我是你,我会这样做。

Bundle bundle = new Bundle();
                bundle.putString("KEY_STRING", "HELLOWORLD");
                fragmentTransaction.setArguments(bundle);

然后从Fragment Class

    String Helloworld =  getArguments().getString("KEY_STRING");
textView.setText(Helloworld);

答案 2 :(得分:0)

找出那里的错误,每当我在搜索视图中输入文本时,新的下拉列表就会创建对象。这就是我得到空对象引用的原因。所以声明为最终变量。解决问题。

@Override                                                                                           
protected void onCreate(Bundle savedInstanceState) {                                                
    super.onCreate(savedInstanceState);                                                             
    setContentView(R.layout.activity_main);                                                         

    final dropdown dropdown = new dropdown();                                                       

    searchView = (SearchView) findViewById (R.id.search_view);                                      
    searchView.setQueryHint("search");                                                              

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {                        
        @Override                                                                                   
        public boolean onQueryTextSubmit(String query) {                                            
            return false;                                                                           
        }                                                                                           

        @Override                                                                                   
        public boolean onQueryTextChange(String newText) {                                          

            if(aBoolean){                                                                           
                RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative);       
                relativeLayout.setVisibility(View.INVISIBLE);                                       
                FragmentManager fragmentManager = getSupportFragmentManager();                      
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();       
                fragmentTransaction.setCustomAnimations(R.anim.top_to_bottom, R.anim.bottom_to_top);
                fragmentTransaction.replace(R.id.fragment_container,dropdown);                      
                fragmentTransaction.commit();                                                       
                aBoolean=false;                                                                     
            }                                                                                       

            else{                                                                                   

                 dropdown.setTextView(R.id.heading1,"Heading1");                                    
            }                                                                                       
            return false;                                                                           
        }                                                                                           
    });}