该怎么办"找不到id ..."在Android活动中使用碎片

时间:2016-03-02 21:32:09

标签: java android xml android-fragments

我希望我的应用程序以large-land模式向用户显示此屏幕,以便他可以在同一屏幕上输入输入,按下按钮,并查看输出。

enter image description here

对于任何其他模式,我希望按顺序一次显示上面显示的两个窗格,其中当用户按下按钮时出现第二个屏幕,之后按下back按钮返回输入"输入"屏幕左侧:

enter image description here enter image description here

我所做的工作直到在第二个屏幕上显示正确的输出但我得到了#34;没有找到id" container然后发生异常

以上第一个屏幕的xml

device(large-land).xml

<LinearLayout                                                                                       xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width=   "match_parent"  android:layout_height=  "match_parent"
    android:id                  ="@+id/linear_layout_container_in_large_land_device_xml"
    android:weightSum           ="3"
    >
    <fragment
        android:name="com.dslomer64.fragments.InputFragment"                android:layout_weight   ="2" android:layout_width="wrap_content" android:layout_height="match_parent"
        android:id              ="@+id/inputFragment"
        tools:layout            ="@layout/input_fragment"
    />
    <fragment
        android:name="com.dslomer64.fragments.OutputFragment"               android:layout_weight   ="1" android:layout_width="wrap_content" android:layout_height="match_parent"
        android:id              ="@+id/outputFragment"
        tools:layout            ="@layout/output_fragment"
    />
</LinearLayout>

device.xml(非 - large-land

<FrameLayout                                                                                        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/container"
/>

input_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
    <TextView
        android:id  ="@+id/tvInMainFragment"                                                        android:layout_width ="wrap_content" android:layout_height="wrap_content"
        android:text="How many?"
        />
    <EditText                                                                                       android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:id="@+id/etxIn_input_fragment"
        android:text="3"
        />
    <Button
        android:id="@+id/btnIn_input_fragment"                                                         android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Press to get output in OtherFragment"
        />
</LinearLayout>

output_fragment.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"                            android:layout_width="match_parent" android:layout_height="match_parent" >
    <TextView
        android:id="@+id/txvIn_output_fragment"                                                                   android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Output will go here in OutputFragment"
        />
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity
{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.device);
        if (findViewById(R.id.container) != null) {
            InputFragment inputFragment;
            inputFragment = new InputFragment();
            getFragmentManager().beginTransaction()
                    .add(R.id.container, inputFragment)
                    .commit();
            }       
     }
}

InputFragment.java

public class InputFragment extends Fragment
{
    public View onCreateView(LayoutInflater     inflater,
                             ViewGroup          viewGroup,
                             Bundle             savedInstanceState)
    {
        View view = inflater.inflate(R.layout.input_fragment, viewGroup, false);
        Button  btn;
        btn = (Button)view.findViewById(R.id.btnIn_input_fragment);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                OutputFragment outputFragment;
                outputFragment = new OutputFragment();
                if (getFragmentManager().findFragmentById(R.id.inputFragment) == null) {
                    getActivity().setContentView(R.layout.device);
                    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.container, outputFragment);
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();
                }
                getActivity().setContentView(R.layout.input_fragment);
                EditText etx = (EditText) getActivity().findViewById(R.id.etxIn_input_fragment);
                fillFragmentOtherWithInfo(etx.getText().toString());
            }
        });
        return view;
    }
    public void fillFragmentOtherWithInfo(String ks)
    {
        getActivity().setContentView(R.layout.output_fragment);
        TextView tvOther = (TextView)getActivity().findViewById(R.id.txvIn_output_fragment);
        String s = "";
        for(int i = 0; i < parseInt(ks); i++)
            s += ("\n" + (char)(i+65));
        tvOther.setText(s);
    }
}

在第二个屏幕上显示输出时,会发生什么事情都很好。它确实显示正确,但后来我立即得到一个前面有此消息的异常,我不知道如何处理它:

03-02 15:59:52.987 7901-7901/com.dslomer64.fragments E/FragmentManager: No view found for id 0x7f070000 (com.dslomer64.fragments:id/container) for fragment OutputFragment{41ecc848 #1 id=0x7f070000}

containerid(非大地)中FrameLayout的{​​{1}},在片段交易的device.xml部分中引用在.add MainActivity中。如果我使用onCreate而不是.replace,我会收到相同的错误。

以下是错误的全文:

.add

1 个答案:

答案 0 :(得分:0)

以下代码生成所需的活动。与Fragment s。

MainActivity.java

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity
{
    public static boolean PORTRAIT_MODE;

    Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        mContext = this;

        setContentView(R.layout.main);

        PORTRAIT_MODE = (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT    );

        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (PORTRAIT_MODE)
                {
                    EditText inputP = (EditText)findViewById(R.id.input_in_main_portrait);
                    Intent  outputActivity;
                            outputActivity = new Intent(mContext, OutputActivity.class);
                            outputActivity.putExtra("numberOfLetters", inputP.getText().toString());
                    startActivity(outputActivity);
                } else
                {
                    EditText inputL  = (EditText)findViewById(R.id.input_in_main_portrait);
                    TextView outputL = (TextView)findViewById(R.id.txvIn_output_fragment);
                    outputL.setText(DoStuff.createOutput(Integer.parseInt(inputL.getText().toString())));
                }
            }
        });
    }
}

DoStuff.java

public class DoStuff
{
        public static String createOutput(int k){
                String s = "";
                for(int i = 0; i < k; i++)
                        s += "\n" + (char)(i+65);
                return s;
        }
}

OutputActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class OutputActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Intent i = getIntent();
        String s = i.getStringExtra("numberOfLetters");
        Log.w("OutputActivity=====", "`````TRYING to be Displaying " + s + " letters in what? Portrait? " + MainActivity.PORTRAIT_MODE);

        setContentView(R.layout.output_fragment);

        TextView et = (TextView) findViewById(R.id.txvIn_output_fragment);
        et.setText(DoStuff.createOutput(Integer.parseInt(s)));
    }
}

InputFragment.java

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class InputFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        super.onCreateView(inflater, parent, savedInstanceState);

        return inflater.inflate(R.layout.input_fragment, parent, false);
    }

}

OutputFragment.java

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class OutputFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        super.onCreateView(inflater, parent, savedInstanceState);

        return inflater.inflate(R.layout.output_fragment, parent, false);
    }
}

input_fragment.xml

<LinearLayout                                                                                       xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
    <TextView
        android:id  ="@+id/tvIn_main_portrait"                                                      android:layout_width ="wrap_content" android:layout_height="wrap_content"
        android:text="How many?"
        />
    <EditText                                                                                       android:layout_width="match_parent" android:layout_height="wrap_content"
        android:id="@+id/input_in_main_portrait"
        android:text="4"
        />
    <Button
        android:id="@+id/btn"                                                                       android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Press to get output"
        />
</LinearLayout>     

output_fragment.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"                            android:layout_width="match_parent" android:layout_height="match_parent" >
    <TextView
        android:id="@+id/txvIn_output_fragment"                                                     android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:text="Output will go here in OutputFragment"
        />
</LinearLayout>

布局\ main.xml中

<LinearLayout                                                                                       xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
    <fragment                                                                                       android:layout_width="match_parent" android:layout_height="match_parent"
        android:id="@+id/main_fragment"

        android:name="com.dslomer64.fragthis.InputFragment"
        />
</LinearLayout>

布局土地\ main.xml中

    <LinearLayout                                                                                       xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"
        android:layout_weight="3"
        >

    <fragment                                                                                           android:layout_width="wrap_content" android:layout_height="match_parent"
        android:id="@+id/input_fragment"
        android:name="com.dslomer64.fragthis.InputFragment"

        />

    <fragment                                                                                           android:layout_width="wrap_content" android:layout_height="match_parent"

        android:id="@+id/output_fragment"

        android:name="com.dslomer64.fragthis.OutputFragment"
        />
    </LinearLayout>