Android studio错误,动态分配Fragment

时间:2016-07-11 03:18:40

标签: android-studio android-fragments

尝试在横向模式下打开其他片段,它在纵向模式下没有问题。任何帮助将不胜感激。

MAIN Activity.java

import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


public class MainActivity extends AppCompatActivity implements LFragment.OnFragmentSendText{

我有两个片段的活动。下面的代码有助于确定是纵向还是横向模式..然后应该执行以下操作。我有两个activity_main.xml文件。一个用于肖像,一个用于陆地。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
            if (savedInstanceState == null){
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new LFragment())
                        .commit();
            }
        } else {
            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new LFragment())
                        .commit();
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container2, new infoFragment(), "fragment2")
                        .commit();
            }
        }
    }

Description within image here

这里我从接口定义一个方法来从第一个片段中检索信息。试图调用infoFragment(第二个片段)但是一旦我实现了这个

我就会收到错误
 (infoFragment)     getSupportFragmentManager().findFragmentByTag("info");

我不确定为什么

    @Override
    public void onSentText(String text) {
        infoFragment info = (infoFragment)     getSupportFragmentManager().findFragmentByTag("info");
        if (info !=null){
            info.setText(text);
        } else {
            infoFragment fragment = new infoFragment();
            Bundle args = new Bundle();
            args.putString("text", text);
            fragment.setArguments(args);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container, fragment)
                    .addToBackStack(null).commit();
            fragment.sentText();
        }
    }
}

Lfragment java class

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class LFragment extends ListFragment {
    OnFragmentSendText mSendText;

    public interface OnFragmentSendText{
        public void onSentText(String text);
    }
    public LFragment(){

    }
    public void onAttach(Activity activity){
        super.onAttach(activity);
        try{
            mSendText = (OnFragmentSendText)activity;
        } catch(ClassCastException e){
            throw new ClassCastException(activity.toString() + " must implement OnFragmentSendText");
        }
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
       //inflate the fragment layout file

         ViewGroup rootView =(ViewGroup)inflater.inflate(R.layout.prof_layout,container,false);
        //create datasource
        String[] datasource = {"A","B","C","D","E"};
        //create adapter
        ArrayAdapter<String> adapter = new ArrayAdapter<String>    (getActivity(),R.layout.row_layout,R.id.txtitem,datasource);
        //bind adapter to the listfragments
        setListAdapter(adapter);
        //retain listfragment instance across configuration changes
        setRetainInstance(true);
        return rootView;
    }

    public void onListItemClick(ListView l, View view, int position, long id){
        ViewGroup viewGroup=(ViewGroup)view;
        TextView txt=(TextView)viewGroup.findViewById(R.id.txtitem);

        String value = (String) l.getItemAtPosition(position);
        mSendText.onSentText(value);




    }
}

infoFragment java class

import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

public class infoFragment extends Fragment {

    TextView degrees;
    ImageView image;
    String stringtext;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         return inflater.inflate(R.layout.info_layout, container, false);
    }
     @Override
     public void onViewCreated(View view, Bundle savedInstanceState){
         image = (ImageView) view.findViewById(R.id.imageView);
         RatingBar rating=(RatingBar) view.findViewById(R.id.ratingBar);
         degrees=(TextView)view.findViewById(R.id.textView);

     }
    public void setText(final String string) {
        degrees.setText(string);

        //conversions...
        }
     }

    public void sentText(){
        new MyTask().execute();

    }

    private class MyTask extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... strings) {
            Bundle b = getArguments();
            stringtext = b.getString("text");
            return null;
        }

        protected void onPostExecute(String result){setText(stringtext);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

如果您使用Fragment,则import android.app.Fragment; 也应来自支持库。

在infoFragment类中替换此导入:

import android.support.v4.app.Fragment;

用这个:

/usr/bin/python