Java Android滑块布局和NullPointerException
好的,我有一个按钮和一个列表视图的主布局,我想做的是当点击按钮时,一些其他的东西会显示在它的顶部(一些其他按钮和文本视图…),所以技术上,按钮会使列表视图和它自己向下滑动,以便其他元素会显示出来。 因此,我创建了一个名为Slider的类,扩展了LinearLayout类,在该类中我编写了制作幻灯片的方法,并在XML文件中使用该类来显示布局 首先,代码如下: Slider.java:Java Android滑块布局和NullPointerException,java,android,layout,slider,Java,Android,Layout,Slider,好的,我有一个按钮和一个列表视图的主布局,我想做的是当点击按钮时,一些其他的东西会显示在它的顶部(一些其他按钮和文本视图…),所以技术上,按钮会使列表视图和它自己向下滑动,以便其他元素会显示出来。 因此,我创建了一个名为Slider的类,扩展了LinearLayout类,在该类中我编写了制作幻灯片的方法,并在XML文件中使用该类来显示布局 首先,代码如下: Slider.java: public class Slider extends LinearLayout{ //The layo
public class Slider extends LinearLayout{
//The layout that'll show up when the slide is done:
public RelativeLayout Show= (RelativeLayout)findViewById(R.id.Show);
public Slider(Context context) {
super(context);
}
//Method that'll make the slide
public void toggle(){
TranslateAnimation animation;
animation = new TranslateAnimation(0.0f, 0.0f, 0.0f, -Show.getHeight());
animation.setDuration(2000);
animation.setInterpolator(new AccelerateInterpolator());
startAnimation(animation);
}
public String toString(){
return "THE TOSTRING....";
}
}
Slider.xml:
<com.example.andtestbdd3.Slider
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/slider"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="5dip">
<RelativeLayout
android:id="@+id/Show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/bouton1">
<EditText
android:id="@+id/Edit1"
android:layout_height="wrap_content"
android:layout_width="130dp"
android:hint="Nom"
android:paddingTop="1dp"
android:paddingBottom="1dp"
android:background="#C2BCBC"
android:maxLength="25"
android:layout_marginRight="20dp"
android:layout_marginBottom="3dp"/>
<EditText
android:id="@+id/Edit2"
android:layout_height="wrap_content"
android:layout_width="130dp"
android:hint="Prenom"
android:paddingTop="1dp"
android:paddingBottom="1dp"
android:layout_toRightOf="@id/Edit1"
android:background="#C2BCBC"
android:maxLength="25"/>
</RelativeLayout>
</com.example.andtestbdd3.Slider>
日志:
Activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
android:background="#000000"
tools:context=".MainActivity" >
<Button
android:id="@+id/bouton1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#595959"
android:gravity="center"
android:paddingBottom="2dp"
android:text="Ajouter" />
<LinearLayout
android:id="@+id/layout"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:layout_marginLeft="17dp"
android:padding="2dp"
android:background="#595959">
<ListView
android:id="@+id/lista"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:background="#7E7E7E"
android:choiceMode="singleChoice"
android:divider="#6F6F6F"
android:dividerHeight="2dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
/>
</LinearLayout>
</RelativeLayout>
因此,当我从xml文件中获取滑块时,没有问题。
但是当我尝试使用类文件中的一些方法时,我得到了NullPointerException!
我希望我的问题是清楚的。如果您需要更多信息,请告诉我,我会尽快更新。
谢谢没有logcat输出,我有点茫然,但在快速浏览之后,我发现您在setContentView方法中使用了activity_main.xml。很高兴看到活动的主布局代码。 我想你的错误可能在这里:
slider = (Slider)findViewById(R.id.slider);
因为findviewbyd(R.id.slider)将在您之前设置的activity_main.xml内查找id为“slider”的视图元素,所以它不会在slider.xml内查找。。。如果找不到,将返回null
编辑:确定,使用logcat输出,现在确认滑块设置为空
编辑2:现在我可以看到您的活动\u主布局:让我一步一步地解释发生了什么:
setContentView(R.layout.activity_main)代码>
android:visibility="gone"
稍后,您可以通过编程再次将visibily设置为visible
真希望这有帮助 在我看来,当您尝试使用
Slider.toString()
从您的logcat系列中可以看出:
com.example.andtestbdd3.MainActivity.onCreate(MainActivity.java:53)
发布活动\u main.xml…已更新。。。。Activity_main.xml addedWhy您认为滑块为空??你有什么建议作为解决方案?@Tha.Poox我添加了一个解决方案建议谢谢你的解释:)但是我找到了另一个更好的方法,我会尽快上传。我在这里找到了另一个最好的方法:[谢谢你的回答伙计们:)[1]:我很高兴听到我的解释有帮助:-)
android:visibility="gone"
com.example.andtestbdd3.MainActivity.onCreate(MainActivity.java:53)