如何更改 android 上的可见相对布局?
How can i change visible relativelayout on android?
试图找到解决方案,但当我尝试这样做时,程序以某种方式崩溃了。
我的代码有什么问题,找不到这样做的任何原因?
无论如何,在程序的其他部分非常相似的工作正常吗?
一点代码来解释我的问题..
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>
这是我来自 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
还有这个
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.RelativeLayout.findViewById(int)' on a null object reference
您的 onCreate 应如下所示:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
switch();
}
您的切换方法应如下所示:
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);
}
由于您的所有观点; chatsLayout、chats 和 noChats 定义在同一个 xml 中,它在您的 activity 中膨胀,您可以直接调用它们的 findViewById。
在onCreate()
中调用setContentView()
并使用findViewById()
找到它。否则你会得到 NullPointerException
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
switch();
}
试图找到解决方案,但当我尝试这样做时,程序以某种方式崩溃了。
我的代码有什么问题,找不到这样做的任何原因?
无论如何,在程序的其他部分非常相似的工作正常吗?
一点代码来解释我的问题..
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>
这是我来自 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
还有这个
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.RelativeLayout.findViewById(int)' on a null object reference
您的 onCreate 应如下所示:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
switch();
}
您的切换方法应如下所示:
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);
}
由于您的所有观点; chatsLayout、chats 和 noChats 定义在同一个 xml 中,它在您的 activity 中膨胀,您可以直接调用它们的 findViewById。
在onCreate()
中调用setContentView()
并使用findViewById()
找到它。否则你会得到 NullPointerException
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
switch();
}