如何在Service类上为linearLayout设置alpha

时间:2016-08-14 15:47:37

标签: android android-layout

我有一个服务类,其中包含WindowManagerWindowManager中包含一个简单的LinearLayoutLinearLayout是空的透明的。我正在尝试为此布局设置alpha值,如下所示:

leftSwipe.getBackground().setAlpha(100)    //leftSwipe is a LinearLayout

但我得到的错误是这样的:

Unable to create service com.example.adarsh.test.Window: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setAlpha(int)' on a null object reference

为什么它给了我NullPointerException,即使LinearLayout WindowManager中也存在LinearLayout。我使用LinearLayout l=new LinearLayout(this); LinearLayout.LayoutParams leftS=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); l.getBackground().setAlpha(100); l.setLayoutParams(leftS); listWindow.addView(l, myParams); //listWindow is my WindowManager 这样:

public class Student
{
    public int ID { get; set; }
    public string FirstName { get; set; }
    public virtual ICollection<Enrollment> Enrollments { get; set; }
}


public class Enrollment
{
    public int EnrollmentID { get; set; }
    public int CourseID { get; set; }
    public int StudentID { get; set; }

    public virtual Course Course { get; set; }
    public virtual Student Student { get; set; }
}


public class Course
{
    public int CourseID { get; set; }
    public string Title { get; set; }

    public virtual ICollection<Enrollment> Enrollments { get; set; }
}

1 个答案:

答案 0 :(得分:0)

  

尝试在空对象引用上调用虚方法'void android.graphics.drawable.Drawable。 setAlpha(int)'

已经告诉你大部分情况。 l.getBackground()返回Drawable个实例或null。在你的情况下,它是null,因此抛出了NullPointerException

确保设置背景或尝试为您的问题实施不同的解决方案。