片段(bug)与无片段的活动(ok)

时间:2014-08-17 14:24:32

标签: java android

我已经为学习目的创建了一个小应用程序。在主活动中有两个按钮:一个用于活动WITH片段,其中包含文本和微调器,第二个用于活动WITHOUT片段,其中包含文本和微调器。

以下是活动WITHOUT片段的代码:

public class NoFragmentActivity extends Activity {

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

    Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
    R.array.planets_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

}

activity_no_fragment.xml文件只是textview和spinner。

这里是活动WITH片段给了我这样一个错误:

http://imgur.com/A4hqdtJ

public class SpinerActivity extends Activity { //with fragment

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_spiner); //shouldn't it be activity_spinner?
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.kontener, new PlaceholderFragment()) //kontener refers to fragment_spiner.xml
                .commit();
    }


    Spinner spinner2 = (Spinner) findViewById(R.id.planets_spinner2);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
            R.array.planets_array, android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(adapter2);

}

Activity_spiner.xml是:

FrameLayout

    (etc...)
    android:id="@+id/container"
    (etc...)

fragment_spiner与activity_no_fragment几乎相同:

RelativeLayout 
    (etc...)
    android:id="@+id/kontener"
    (etc...)

TextView
    android:id="@+id/tekstspiner"
    (etc...) 
Spinner
    android:id="@+id/planets_spinner2"
   (etc...)

我有些问题我无法解决:

  1. 为什么我有这样的错误?我该如何修复代码?
  2. 在此行的SpinnerActivity文件中:setContentView(R.layout.fragment_spiner);不应该有activity_spinner而不是fragment_spinner?
    我在没有微调器的情况下创建的片段的另一个空白活动在这个地方有activity_blank(不像我的应用程序那样* _fragment)并且工作正常。
    当我改变它activity_spinner我有NPE。为什么?我该如何解决?
  3. 编辑:这是包含类和xml文件的zip:https://www.sendspace.com/file/ofh8yi

1 个答案:

答案 0 :(得分:0)

在spinerActivity中,您将两次设置相同的布局。一个用于活动,另一个用于fragemnt。这就是为什么你看到它两次

        public class SpinerActivity extends Activity {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.fragment_spiner);  // First one
                if (savedInstanceState == null) {
                    getFragmentManager().beginTransaction()
                            .add(R.id.kontener, new PlaceholderFragment())
                            .commit();
                }


                Spinner spinner2 = (Spinner) findViewById(R.id.planets_spinner2);
        // Create an ArrayAdapter using the string array and a default spinner layout
                ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this,
                        R.array.planets_array, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
                adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
                spinner2.setAdapter(adapter2);

            }



            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.spiner, menu);
                return true;
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();
                if (id == R.id.action_settings) {
                    return true;
                }
                return super.onOptionsItemSelected(item);
            }

            /**
             * A placeholder fragment containing a simple view.
             */
            public static class PlaceholderFragment extends Fragment {

                public PlaceholderFragment() {
                }

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_spiner, container, false);  //second one 
                    return rootView;
                }
            }
        }