尝试在空对象引用上调用虚方法'void android.view.ViewGroup.setVisibility(int)'

时间:2015-12-09 01:55:53

标签: android android-animation android-cardview android-transitions

我尝试使用CardView使用ToogleButton实现可扩展/折叠Animating Layout Changes,允许用户隐藏或显示CardView内的内容。当我尝试运行该应用时,Android Studio向我显示了以下消息:

FATAL EXCEPTION: main
                                                   Process: com.appmac.ron.expandcards, PID: 29977
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appmac.ron.expandcards/com.appmac.ron.expandcards.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.setVisibility(int)' on a null object reference
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:135)
                                                       at android.app.ActivityThread.main(ActivityThread.java:5221)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.setVisibility(int)' on a null object reference
                                                       at com.appmac.ron.expandcards.MainActivity.onCreate(MainActivity.java:36)
                                                       at android.app.Activity.performCreate(Activity.java:5933)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
                                                       at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:135) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:5221) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at java.lang.reflect.Method.invoke(Method.java:372) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

MainActivity.java

package com.appmac.ron.expandcards;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

    private ViewGroup mContainerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        mContainerView = (ViewGroup) findViewById(R.id.container);
        mContainerView.setVisibility(View.INVISIBLE);

    }

    public void changeVisibility(View view){

        boolean checked = ((ToggleButton)view).isChecked();
        if(checked){
            mContainerView.setVisibility(View.VISIBLE);
        }
        else{
            mContainerView.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.appmac.ron.expandcards">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


    </application>

</manifest>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.appmac.ron.expandcards.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.appmac.ron.expandcards.MainActivity"
        tools:showIn="@layout/activity_main">

        <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
            android:id="@+id/cardFacebook"
            android:layout_width="fill_parent"
            android:layout_height="150dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:background="?attr/selectableItemBackground"
            android:clickable="true"
            card_view:cardCornerRadius="0dp">

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:id="@+id/relativeLayoutTop">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="TOP"
                    android:id="@+id/textViewTop" />

                <ToggleButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textOff="Show More"
                    android:textOn="Hide"
                    android:id="@+id/toggleButton"
                    android:layout_centerVertical="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true"
                    android:backgroundTint="#fbfbfb"
                    android:textColor="#006199"
                    android:onClick="changeVisibility"/>

            </RelativeLayout>


            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="100dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="false"
                android:layout_below="@+id/container"
                android:layout_marginTop="50dp"
                android:divider="?android:dividerHorizontal"
                android:animateLayoutChanges="true">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="BOTTOM"
                    android:id="@+id/textViewBottom" />
            </RelativeLayout>


        </android.support.v7.widget.CardView>



    </RelativeLayout>

<

/android.support.design.widget.CoordinatorLayout>

关于TranistionLayout: http://developer.android.com/intl/es/training/animation/layout.html

坦克提供任何帮助!

1 个答案:

答案 0 :(得分:2)

除非我错过了它,看起来你没有在任何地方定义容器ID,只有引用似乎在你的相对布局的layout_below标签中。

编辑:我会发表评论但我还无法发表评论

如果您尝试引用RelativeLayout,那么

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="false"
        android:id="@+id/container"
        android:layout_marginTop="50dp"
        android:divider="?android:dividerHorizontal"
        android:animateLayoutChanges="true">

应该修复它,否则包括

android:id="@+id/container

标记您要查找的内容