简体   繁体   中英

How can i change visible relativelayout on android?

Tried to find solution, somehow program get crashed when i try to do this.

What is wrong with this my code, can't find any reason why it is doing this?

Anyways, on other part of program pretty close similar works correctly?

Little bit code to explain my problem..

my.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    switch();
}

public void switch() {
    RelativeLayout chatsLayout = (RelativeLayout)findViewById(R.id.chatsLayout);
    RelativeLayout chats = (RelativeLayout) chatsLayout.findViewById(R.id.chats);
    chats.setVisibility(View.GONE);

    RelativeLayout noChats = (RelativeLayout) chatsLayout.findViewById(R.id.noChats);
    noChats.setVisibility(View.GONE);
}

my.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/chatsLayout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white">
    <RelativeLayout
        android:id="@+id/chats"
        android:visibility="gone"
        android:padding="8dp"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/noChats"
        android:padding="8dp"
        android:visibility="visible"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </RelativeLayout>
</RelativeLayout>

Here is my error code from logcat:

08-10 08:06:00.922    2736-2736/fi.hgs.apps E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: fi.hgs.apps, PID: 2736
java.lang.RuntimeException: Unable to start activity ComponentInfo{fi.hgs.apps/fi.hgs.apps.ChatsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.RelativeLayout.findViewById(int)' on a null object reference

And this

 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.RelativeLayout.findViewById(int)' on a null object reference

Your onCreate should look like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
    switch();
}

Your switch method should look like this:

public void switch() {
    RelativeLayout chatsLayout = (RelativeLayout)findViewById(R.id.chatsLayout);
    RelativeLayout chats = (RelativeLayout)findViewById(R.id.chats);
    chats.setVisibility(View.GONE);

    RelativeLayout noChats = (RelativeLayout)findViewById(R.id.noChats);
    noChats.setVisibility(View.GONE);
}

Since all your views; chatsLayout, chats and noChats are defined in the same xml which is inflated in your activity, you can directly call findViewById on them.

call setContentView() in onCreate() and find it using findViewById() . Otherwise you will get a NullPointerException

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);
        switch();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM