gpt4 book ai didi

java - Android slider 布局和 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 03:06:33 25 4
gpt4 key购买 nike

好的,所以我有一个带有按钮和 ListView 的主布局,我想做的是当点击按钮时,其他一些东西会显示在它的顶部(其他一些按钮和 TextView .. .),所以从技术上讲,按钮将使 Listview 及其本身向下滑动,以便显示其他元素。所以我创建了一个名为 Slider 的类,它扩展了 LinearLayout 类,我在其中编写了制作幻灯片的方法,并且我在 XML 文件中使用了这个类来显示布局。

首先,这是代码:

slider .java:

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 .java:

public class MainActivity extends Activity {

Slider slider = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the slider from the XML file which uses the Slider class (Slider.java)
slider = (Slider)findViewById(R.id.slider);
//everything is ok till here, When I try to use some stuff from the Slider, //the NullPointerEXCEPTION
//Will show up, for example; if I call the toString methode:
Toast.makeText(this,"Le text: "+slider.toString(), Toast.LENGTH_LONG).show();
}

}

日志:

02-12 09:07:20.870: D/AndroidRuntime(933): Shutting down VM
02-12 09:07:20.870: W/dalvikvm(933): threadid=1: thread exiting with uncaught exception (group=0xb4aaaba8)
02-12 09:07:20.890: E/AndroidRuntime(933): FATAL EXCEPTION: main
02-12 09:07:20.890: E/AndroidRuntime(933): Process: com.example.andtestbdd3, PID: 933
02-12 09:07:20.890: E/AndroidRuntime(933): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.andtestbdd3/com.example.andtestbdd3.MainActivity}: java.lang.NullPointerException
02-12 09:07:20.890: E/AndroidRuntime(933): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-12 09:07:20.890: E/AndroidRuntime(933): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-12 09:07:20.890: E/AndroidRuntime(933): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-12 09:07:20.890: E/AndroidRuntime(933): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-12 09:07:20.890: E/AndroidRuntime(933): at android.os.Handler.dispatchMessage(Handler.java:102)
02-12 09:07:20.890: E/AndroidRuntime(933): at android.os.Looper.loop(Looper.java:136)
02-12 09:07:20.890: E/AndroidRuntime(933): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-12 09:07:20.890: E/AndroidRuntime(933): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 09:07:20.890: E/AndroidRuntime(933): at java.lang.reflect.Method.invoke(Method.java:515)
02-12 09:07:20.890: E/AndroidRuntime(933): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-12 09:07:20.890: E/AndroidRuntime(933): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-12 09:07:20.890: E/AndroidRuntime(933): at dalvik.system.NativeStart.main(Native Method)
02-12 09:07:20.890: E/AndroidRuntime(933): Caused by: java.lang.NullPointerException
02-12 09:07:20.890: E/AndroidRuntime(933): at com.example.andtestbdd3.MainActivity.onCreate(MainActivity.java:53)
02-12 09:07:20.890: E/AndroidRuntime(933): at android.app.Activity.performCreate(Activity.java:5231)
02-12 09:07:20.890: E/AndroidRuntime(933): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-12 09:07:20.890: E/AndroidRuntime(933): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-12 09:07:20.890: E/AndroidRuntime(933): ... 11 more

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 文件中获取 Slider 时,没问题。但是当我尝试使用类文件中的一些方法时,我得到了 NullPointerException !我希望我的问题很清楚。如果您需要更多信息,请告诉我,我会尽快更新。谢谢

最佳答案

在我看来,当您尝试使用 slider.toString() 时,您的 Slider 对象未找到并且为 null从您的 logcat 行可以看出:

com.example.andtestbdd3.MainActivity.onCreate(MainActivity.java:53)

关于java - Android slider 布局和 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21730064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com