Android - 相对布局中的空指针引用

时间:2016-01-08 04:33:33

标签: android

我做了一个小班来改变布局大小。

这是班级:

import android.app.Activity;
import android.util.DisplayMetrics;


public class LayoutResize extends Activity{

    public DisplayMetrics displayMetrics = new DisplayMetrics();

    public int width(int sizeX, DisplayMetrics displayMetrics)
    {
        int screenWidth = displayMetrics.widthPixels * sizeX / 100;
        return screenWidth;
    }

    public int height(int sizeY, DisplayMetrics displayMetrics)
    {
        int screenHeight = displayMetrics.heightPixels * sizeY / 100;
        return screenHeight;
    }
}

我在MainActivity

中以这种方式使用它
public class MainActivity extends AppCompatActivity {

    DisplayMetrics displayMetrics = new DisplayMetrics();
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        LayoutResize layoutResize = new LayoutResize();

        RelativeLayout relativeViewPager =(RelativeLayout) findViewById(R.id.relativeViewPager);
        int relativeViewPagerHeight = layoutResize.height(70, displayMetrics);
        ViewGroup.LayoutParams viewPagerParams = relativeViewPager.getLayoutParams();
        viewPagerParams.height = relativeViewPagerHeight;
        relativeViewPager.setLayoutParams(viewPagerParams);


        RelativeLayout headerLayout =(RelativeLayout) findViewById(R.id.headerLayout);
        int headerLayoutHeight = layoutResize.height(70, displayMetrics);
        ViewGroup.LayoutParams headerLayoutParams = headerLayout.getLayoutParams();
        headerLayoutParams.height = headerLayoutHeight;
        headerLayout.setLayoutParams(headerLayoutParams);
}

返回Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.widget.RelativeLayout.getLayoutParams()' on a null object reference

如果我只使用一次,例如:

RelativeLayout relativeViewPager =(RelativeLayout) findViewById(R.id.relativeViewPager);
            int relativeViewPagerHeight = layoutResize.height(70, displayMetrics);
            ViewGroup.LayoutParams viewPagerParams = relativeViewPager.getLayoutParams();
            viewPagerParams.height = relativeViewPagerHeight;
            relativeViewPager.setLayoutParams(viewPagerParams);

它有效,但是如果我像我所显示的那样使用它两次,它会抛出空对象引用..

ViewPager

中的项目的XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" >

    <RelativeLayout
        android:id="@+id/headerLayout"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >

        <ImageView
            android:id="@+id/slidingBackground"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true" />

        <RelativeLayout
            android:id="@+id/headerText"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/titleText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/messageText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_below="@+id/titleText" />

        </RelativeLayout>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/slidingImageWrapper"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/headerLayout">

        <ImageView
            android:id="@+id/slidingImage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </RelativeLayout>


</RelativeLayout>

这是主类本身的XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/relativeViewPager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:fillViewport="true"
        android:measureAllChildren="false"
        android:scrollbars="none" >

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true" />

    </RelativeLayout>



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_below="@+id/relativeViewPager">

        <com.viewpagerindicator.CirclePageIndicator
            android:id="@+id/indicator"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:gravity="bottom"
            android:padding="10dip"
            app:centered="true"
            app:fillColor="#ff0099"
            app:pageColor="#FF0000"
            app:snap="false" />

        <Button
            android:id="@+id/loginButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Login"
            android:textColor="#ffffff"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="10dp"
            android:layout_marginBottom="10dp"
            android:background="@drawable/login_button" />

        <Button
            android:id="@+id/signUpButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Signup"
            android:textColor="#ffffff"
            android:layout_alignTop="@+id/loginButton"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="63dp"
            android:layout_marginEnd="63dp"
            android:background="@drawable/signup_button" />

    </RelativeLayout>


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我做了一个愚蠢的错误,没有夸大我使用的视图(我使用另一个我忘了膨胀的xml)

不得不补充:

LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View pagerItemInflater = (View) inflater.inflate(R.layout.viewpager_item, null);

并像这样使用它:

RelativeLayout headerLayout = (RelativeLayout) pagerItemInflater.findViewById(R.id.headerLayout);