装卸装置旋转

时间:2019-09-24 20:02:03

标签: java android

我试图让我的应用处理设备旋转,但是当我从mainActivity的onCreate方法上添加以下代码时,它总是崩溃。这是我得到的错误。我该如何解决? :

  

java.lang.NullPointerException:尝试调用虚拟方法'void   空对象引用上的android.widget.ArrayAdapter.clear()'

2019-09-25 06:58:16.743 10444-10444/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplication, PID: 10444
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ArrayAdapter.clear()' on a null object reference
        at com.example.myapplication.MainActivity$1.onChanged(MainActivity.java:48)
        at com.example.myapplication.MainActivity$1.onChanged(MainActivity.java:44)
        at androidx.lifecycle.LiveData.considerNotify(LiveData.java:113)
        at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:131)
        at androidx.lifecycle.LiveData.setValue(LiveData.java:289)
        at androidx.lifecycle.LiveData$1.run(LiveData.java:91)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        contactListView = (ListView) findViewById(R.id.contactsListView);
        adapter = new ArrayAdapter<Contact>(this,android.R.layout.simple_list_item_1, contacts);
        contactListView.setAdapter(adapter);
        contactListView.setOnItemClickListener(this);
        setContentView(R.layout.activity_main);
}

3 个答案:

答案 0 :(得分:0)

当设备旋转时,android会取消活动并重新创建它。因此,整个生命周期都被称为。

因此将所有内容保存在onSavedInstanceState中,然后在onRetainInstanceState中提取数据

有关更多详细信息,请点击此处:https://developer.android.com/guide/topics/resources/runtime-changes

在这里我也相信这里的问题是在contactListView =(ListView)findViewById(R.id.contactsListView)之后调用setContentView(R.layout.activity_main)

尝试

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    contactListView = (ListView) findViewById(R.id.contactsListView);
    adapter = new ArrayAdapter<Contact>(this,android.R.layout.simple_list_item_1, contacts);
    contactListView.setAdapter(adapter);
    contactListView.setOnItemClickListener(this);
}

应该可以

答案 1 :(得分:0)

处理生命周期的变化是一个痛苦的过程。我强烈建议您看一下架构组件ViewModel。它将处理方向更改和其他生命周期更改,例如超级按钮。与DataBinding一起添加,您将拥有一个更强大的应用程序,它将为您自动处理生命周期更改。

答案 2 :(得分:0)

您最好将LiveData放在ViewModel中,并在活动中(可能是onCreate()方法中进行观察)。让我向您展示如何在ViewModel中正确使用LiveData,

YourViewModel

public class YourViewModel extends ViewModel {

    // Create a LiveData with a List of Contact
    private MutableLiveData<List<Contact>> contactList = new MutableLiveData<>();

    // encapsulated with immutable live data
    public LiveData<List<Contact>> getContactList() {
        return contactList;
    }

    // call this method with a list of new contacts whenever you need to refresh your list
    public void updateContactList(List<Contact> list) {
        contactList.setValue(list);
    }

    // Rest of the ViewModel...
}

您的活动

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        contactListView = (ListView) findViewById(R.id.contactsListView);

        yourViewModel = ViewModelProviders.of(this).get(YourViewModel.class);

        if(contacts==null) contacts = new ArrayList<>();

        adapter = new ArrayAdapter<Contact>(this,android.R.layout.simple_list_item_1, contacts);
        contactListView.setAdapter(adapter);
        contactListView.setOnItemClickListener(this);

        yourViewModel.getContactList().observe(this, contactObserver);
}


Observer<List<Contact>> contactObserver = new Observer<List<Contact>>() {
        @Override
        public void onChanged(@Nullable List<Contact> newContacts) {
            if(adapter != null && newContacts != null) {
                adapter.clear();
                adapter.addAll(newContacts);
            }
        }
};

以上述方式确保liveData与活动生命周期相关联,并且在adapter方法中它获得非空的onChanged()。希望我的回答能帮助您解决问题。