更改导航抽屉页面时使 ScrollView 消失

Make ScrollView Gone when changing Nav Drawer Pages

我希望我的 scrollview 将其可见性设置为消失,当我选择了我的导航抽屉页面之一时,我试图在一些帮助下完成它但是得到了 nullpointerexecptions 并且我得到了错误 "Cannot Resolve Method setVisibility(int)" 并且 Scrollview 不能已解决,

主要活动

package nota.outlawsindex;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {

private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
Toolbar toolbar;
Scrollview scrollView;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
    android.support.v7.app.ActionBarDrawerToggle mDrawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTitle = mDrawerTitle = getTitle();
    mNavigationDrawerItemTitles =         getResources().getStringArray(R.array.navigation_drawer_items_array);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);
    ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);


    setupToolbar();

    DataModel[] drawerItem = new DataModel[4];//need to update this if you     add start counting at 0

    drawerItem[0] = new DataModel(R.drawable.ic_connect, "Calculate     Sentence");
    drawerItem[1] = new DataModel(R.drawable.ic_fixtures, "Felonies");
    drawerItem[2] = new DataModel(R.drawable.ic_table, "Misdemeanors");
    drawerItem[3] = new DataModel(R.drawable.ic_drawer, "Infractions");
    //add on to the list to create more pages
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    getSupportActionBar().setHomeButtonEnabled(true);

    DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.list_view_item_row, drawerItem);
    mDrawerList.setAdapter(adapter);
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    setupDrawerToggle();



}

private class DrawerItemClickListener implements ListView.OnItemClickListener {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }

}

private void selectItem(int position) {



    scrollView.setVisibility(View.GONE);

    Fragment fragment = null;

    switch (position) {
        case 0:
            fragment = new ConnectFragment();
            break;
        case 1:
            fragment = new FixturesFragment();
            break;
        case 2:
            fragment = new TableFragment();
            break;
        case 3:
            fragment = new SelfAddedFragment();
            break;
        //add more cases if you add more pages

        default:
            break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame,             fragment).commit();

        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(mNavigationDrawerItemTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);

    } else {
        Log.e("MainActivity", "Error in creating fragment");
    }
}

@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) {
    //
    int id = item.getItemId();
    //
    if (mDrawerToggle.onOptionsItemSelected(item)) {

        return true;
    } else if (id == R.id.action_settings) {
        Toast.makeText(getApplicationContext(), "Settings", Toast.LENGTH_SHORT).show();
        return true;
    }
    Toast.makeText(getApplicationContext(), "Search", Toast.LENGTH_SHORT).show();

    return super.onOptionsItemSelected(item);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getSupportActionBar().setTitle(mTitle);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

void setupToolbar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //getSupportActionBar().setDisplayShowHomeEnabled(true);
}

void setupDrawerToggle() {
    mDrawerToggle = new android.support.v7.app.ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name, R.string.app_name);
    //This is necessary to change the icon of the Drawer Toggle upon state change.
    mDrawerToggle.syncState();
}

}

Activity_Main

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundColor"
android:visibility="visible">
    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/relativeLayout"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <RelativeLayout
            android:id="@+id/container_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        </RelativeLayout>


        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/container_toolbar"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">

        </FrameLayout>

        <include
            android:id="@+id/toolbar"
            layout="@layout/toolbar"
            android:layout_gravity="right|top"
            android:layout_below="@+id/scrollView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="26dp"
            android:layout_marginStart="26dp" />

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/scrollView"
            android:layout_centerVertical="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/content_frame"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:id="@+id/relativeLayout2"
                android:focusable="false">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="              Welcome to The Outlaw&apos;s Index! An app designed for individuals who want to educate themselves with knowledge regarding different types of offenses in the United States. Currently this app is directed towards individuals in the United States, but a Canadian version may be released in upcoming months. This app gives users the tools and resources to research different classes of offenses and the repercussions one would receive if convicted. Although this app is informative, advice from law enforcement, attorneys, and other members of the law should be considered more accurate as it may suit your personal needs regarding a specific case. To start off, choose any of the classes on the left hand side, or calculate a sentence and the repercussions that would follow."
                    android:id="@+id/textView2"
                    android:textColor="#ffffff"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:password="false"
                    android:phoneNumber="false"
                    android:singleLine="false"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="33dp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:text="Welcome"
                    android:id="@+id/textView"
                    android:textColor="#ffffff"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginLeft="27dp"
                    android:layout_marginStart="27dp" />

            </RelativeLayout>
        </ScrollView>

    </RelativeLayout>

</RelativeLayout>

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/menuBackgroundColor"
    android:choiceMode="singleChoice"
    android:divider="@color/colorAccent"
    android:dividerHeight="1dp"
    android:paddingTop="15dp"/>
<!--    because there is no header in this one I am using android:paddingTop="15dp"
    to push the menu below the level of the translucent top bar.-->

这是 API 15,我也是 android 开发的新手

更改此行:

ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);

到这一行:

scrollView = (ScrollView) findViewById(R.id.scrollView);