Java 该怎么办;找不到id的视图…”;在Android活动中使用片段

Java 该怎么办;找不到id的视图…”;在Android活动中使用片段,java,android,xml,android-fragments,Java,Android,Xml,Android Fragments,我想让我的应用程序在large land模式下向用户显示此屏幕,以便用户可以在同一屏幕上输入、按下按钮和查看所有输出 对于任何其他模式,我希望上面显示的两个窗格按此顺序一次显示一个,当用户按下按钮时,第二个屏幕出现,然后按下后退按钮返回左侧的“输入”屏幕: 我所做的工作一直到在第二个屏幕上显示正确的输出,但随后我得到“找不到id的视图”container,然后出现异常 下面是上面第一个屏幕的xml: 设备(大面积).xml <LinearLayout

我想让我的应用程序在
large land
模式下向用户显示此屏幕,以便用户可以在同一屏幕上输入、按下按钮和查看所有输出

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

我所做的工作一直到在第二个屏幕上显示正确的输出,但随后我得到“找不到id的视图”
container
,然后出现异常

下面是上面第一个屏幕的
xml

设备(大面积).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>
<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>
<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>
input_fragment.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>
<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>
<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();
            }       
     }
}
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);
    }
}
InputFragment.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();
            }       
     }
}
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);
    }
}

下面的代码生成所需的活动。带有
片段
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())));
                }
            }
        });
    }
}
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)));
    }
}
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;
        }
}
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);
    }

}
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);
    }
}
OutputFragment.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);
    }

}
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>     
    <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>

output_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>     
    <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>

layout\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>

layout land\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>

这可能会对您有所帮助。非常相似