3

我想为不同的方向模式构建具有不同布局的应用程序。所以我建立了我的项目,同样做了 android tutorial [example here]。但是有一个区别,当我想要多种方向模式(横向、纵向等)时,本教程是为不同的尺寸(正常和大)创建的。现在我NullPointerException在方向变化期间得到了一个。这是我的代码:

标题片段:

public class HeadlinesFragment extends ListFragment {

   OnHeadlineSelectedListener HeadlinesCallback;
   List<Person> personLista = new ArrayList<Person>();

   public interface OnHeadlineSelectedListener {
        public void onArticleSelected(Person person);
    }

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

        int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ?
                android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1;

        //TESTOWE WYPENIENIE
        for(int i=0; i<10; i++){
            personLista.add(new Person("Imie"+i+"Nazwisko"+i,"email"+i+"@example"+i+".com"));
        }
        String[] niceTime = new String[personLista.size()];
        for(int i=0; i<personLista.size(); i++){
            niceTime[i] = personLista.get(i).getName();
        }
        //KONIEC

        setListAdapter(new ArrayAdapter<String>(getActivity(), layout, niceTime));
    }
    @Override
    public void onStart() {
        super.onStart();

        // When in two-pane layout, set the listview to highlight the selected list item
        // (We do this during onStart because at the point the listview is available.)
        if (getFragmentManager().findFragmentById(R.id.person_view) != null) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }
    }

    @Override
    public void onAttach(Activity activity){
        super.onAttach(activity);

        try{
            HeadlinesCallback = (OnHeadlineSelectedListener)activity;
        }catch(ClassCastException e){
            throw new ClassCastException(activity.toString()
                    + " have to implement OnHeadlineSelectedListener");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id){
        HeadlinesCallback.onArticleSelected(personLista.get(position));

        getListView().setItemChecked(position, true);
    }

}

人物片段:

public class PersonFragment extends Fragment{
   public static Person person;
   ImageView icon;
   TextView text1;
   TextView text2;
   TextView text3;

    View view;
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {

       if(savedInstanceState != null){
           person = (Person)savedInstanceState.getSerializable("person");
       }

       view = inflater.inflate(R.layout.person_view, container, false);

       icon = (ImageView)view.findViewById(R.id.icon);
       text1 = (TextView)view.findViewById(R.id.name);
       text2 = (TextView)view.findViewById(R.id.email);
       text3 = (TextView)view.findViewById(R.id.info);

       return view;
    }
    @Override
    public void onStart() {
        super.onStart();

        Bundle args = getArguments();
        if (args != null) {
            person = (Person)args.getSerializable("context");

            text1.setText(person.getName());
            text2.setText(person.getEmail());
            text3.setText("Quisque semper ultricies iaculis. Vestibulum nec metus adipiscing, egestas sem mattis, ornare nulla. Quisque nec nibh nec lacus euismod gravida. Proin at pretium enim. In auctor, nisi sit amet pharetra facilisis, est justo mattis mi, eget mattis tortor ligula non orci. Vestibulum neque augue, elementum vitae aliquam non, condimentum ac dui. Nullam ultricies velit nec diam aliquam, sed lacinia sem eleifend. Quisque quam sapien, sodales id suscipit nec, consequat vel nunc. Quisque eu velit nisi. Donec varius consequat metus, tempor semper tortor ultricies vel. Mauris eget arcu nisi. Proin sit amet dui nisl. Quisque auctor lobortis augue sit amet egestas. Fusce mattis aliquam ante, et bibendum nibh ullamcorper et. Integer ipsum nibh, placerat a felis ut, accumsan rutrum eros. Quisque lacinia facilisis ipsum sed commodo.");
            icon.setImageResource(R.drawable.ic_launcher);

        }else{
            if(person==null){
                person = new Person("Test Testowy", "Testowy@gmail.com");
            }
            updateArticleView(person);
        }
    }
    @Override
    public void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);
        outState.putSerializable("person", person);
    }
    public void updateArticleView(Person externalPerson) {
        this.person = externalPerson;

        text1.setText(externalPerson.getName());
        text2.setText(externalPerson.getEmail());
        text3.setText("Quisque semper ultricies iaculis. Vestibulum nec metus adipiscing, egestas sem mattis, ornare nulla. Quisque nec nibh nec lacus euismod gravida. Proin at pretium enim. In auctor, nisi sit amet pharetra facilisis, est justo mattis mi, eget mattis tortor ligula non orci. Vestibulum neque augue, elementum vitae aliquam non, condimentum ac dui. Nullam ultricies velit nec diam aliquam, sed lacinia sem eleifend. Quisque quam sapien, sodales id suscipit nec, consequat vel nunc. Quisque eu velit nisi. Donec varius consequat metus, tempor semper tortor ultricies vel. Mauris eget arcu nisi. Proin sit amet dui nisl. Quisque auctor lobortis augue sit amet egestas. Fusce mattis aliquam ante, et bibendum nibh ullamcorper et. Integer ipsum nibh, placerat a felis ut, accumsan rutrum eros. Quisque lacinia facilisis ipsum sed commodo.");
        icon.setImageResource(R.drawable.ic_launcher);

    }

}

和主要活动:

public class MainActivity extends FragmentActivity implements OnHeadlineSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(findViewById(R.id.fragment_container)!=null){

        if (savedInstanceState != null) {
            return;
        }

        getSupportFragmentManager().beginTransaction()
            .add(R.id.fragment_container, new HeadlinesFragment()).commit();

    }


}

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

    PersonFragment fragment = (PersonFragment)getSupportFragmentManager().findFragmentById(R.id.person_view);

    if(fragment!=null){
        fragment.updateArticleView(person);
    }else{
        PersonFragment newFragment = new PersonFragment();

        Bundle args = new Bundle();
        args.putSerializable("context", person);
        newFragment.setArguments(args);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        transaction.commit();
    }
}

}

问题:一开始一切都很好,但是当我将方向模式更改为横向(在模拟器中 ctrl+f12 中)并再次回到端口并从列表视图中选择一些东西时,我得到“不幸的是应用程序已停止”。

它是 logcat:

11-02 14:29:03.396: E/AndroidRuntime(802): FATAL EXCEPTION: main
11-02 14:29:03.396: E/AndroidRuntime(802): java.lang.NullPointerException
11-02 14:29:03.396: E/AndroidRuntime(802):  at com.example.friendslist.PersonFragment.updateArticleView(PersonFragment.java:68)
11-02 14:29:03.396: E/AndroidRuntime(802):  at com.example.friendslist.MainActivity.onArticleSelected(MainActivity.java:43)
11-02 14:29:03.396: E/AndroidRuntime(802):  at com.example.friendslist.HeadlinesFragment.onListItemClick(HeadlinesFragment.java:69)
11-02 14:29:03.396: E/AndroidRuntime(802):  at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
11-02 14:29:03.396: E/AndroidRuntime(802):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)
11-02 14:29:03.396: E/AndroidRuntime(802):  at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
11-02 14:29:03.396: E/AndroidRuntime(802):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
11-02 14:29:03.396: E/AndroidRuntime(802):  at android.widget.AbsListView$1.run(AbsListView.java:3423)
11-02 14:29:03.396: E/AndroidRuntime(802):  at android.os.Handler.handleCallback(Handler.java:725)
11-02 14:29:03.396: E/AndroidRuntime(802):  at android.os.Handler.dispatchMessage(Handler.java:92)
11-02 14:29:03.396: E/AndroidRuntime(802):  at android.os.Looper.loop(Looper.java:137)
11-02 14:29:03.396: E/AndroidRuntime(802):  at android.app.ActivityThread.main(ActivityThread.java:5041)
11-02 14:29:03.396: E/AndroidRuntime(802):  at java.lang.reflect.Method.invokeNative(Native Method)
11-02 14:29:03.396: E/AndroidRuntime(802):  at java.lang.reflect.Method.invoke(Method.java:511)
11-02 14:29:03.396: E/AndroidRuntime(802):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-02 14:29:03.396: E/AndroidRuntime(802):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-02 14:29:03.396: E/AndroidRuntime(802):  at dalvik.system.NativeStart.main(Native Method)

我的主要问题是我一直在寻找类似“由”之类的东西,在这里我没有。有人可以告诉我哪里有问题吗?

PS 在普通的 android 示例中,当我将子文件夹的名称从 layout-large 更改为 layout-land 时,我遇到了同样的问题。

活动:

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

个人视图:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="72dp"
        android:layout_height="74dp"
        android:src="@drawable/ic_launcher" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Imie i nazwisko" />
            <TextView
                android:id="@+id/email"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="email@sample.com" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

<TextView
    android:id="@+id/info"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="info" />

土地子文件夹中的主要活动:

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

<fragment android:name="com.example.friendslist.HeadlinesFragment"
          android:id="@+id/headlines_fragment"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent" />

<fragment android:name="com.example.friendslist.PersonFragment"
          android:id="@+id/person_view"
          android:layout_weight="2"
          android:layout_width="0dp"
          android:layout_height="match_parent" />

4

1 回答 1

1

好的,在解决这个问题之后,我找到了解决方案,我将onArticleSelected方法中的条件从更改(fragment!=null)findViewById(R.id.fragment_container)。我不知道为什么,在方向改变片段仍然存在之后,也许有人可以解释一下。

EDIT:// 如果有人想要检查方向设置,再次检查某个元素是否等于 null,您可以更简单地进行操作,例如:getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE

于 2013-11-02T19:03:29.043 回答