gpt4 book ai didi

java - Android:将动画插入对话框

转载 作者:太空宇宙 更新时间:2023-11-04 15:22:59 24 4
gpt4 key购买 nike

我想使用对话框来休息 onitemclick。我还想在对话框中嵌入小动画。我已经这样做了,但它显示了我的运行时错误。

public class Basic extends Activity {
final Context context = this;
EditText inputSearch;
private ListView lv;
ArrayAdapter<String> adapter;
AnimationDrawable frameAnimation;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_word);
// storing string resources into Array
String[] adobe_products = getResources().getStringArray(R.array.basic_words);

adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.label, adobe_products);
Arrays.sort(adobe_products, String.CASE_INSENSITIVE_ORDER);
// ListView lv = getListView();
lv = (ListView) findViewById(R.id.list);
inputSearch = (EditText) findViewById(R.id.inputSearch);
lv.setAdapter(adapter);

inputSearch.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Basic.this.adapter.getFilter().filter(cs);
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
// listening to single list item on click
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemValue = (String) parent.getItemAtPosition(position);
final Dialog dialog = new Dialog(context);

switch (position) {

case 10:
dialog.setContentView(R.layout.anim);
ImageView image1 = (ImageView) dialog.findViewById(R.id.anim_image);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
dialog.setTitle(itemValue);
image1.setImageResource(R.drawable.spin_animation);
frameAnimation = (AnimationDrawable) image1.getBackground();

// Start the animation (looped playback by default).
frameAnimation.start();
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();

}

}
});
}
}

日志文件如下所示:

11-26 06:53:18.983: E/AndroidRuntime(1734): FATAL EXCEPTION: main
11-26 06:53:18.983: E/AndroidRuntime(1734): java.lang.NullPointerException
11-26 06:53:18.983: E/AndroidRuntime(1734): at org.example.ihear.Basic$2.onItemClick(Basic.java:94)
11-26 06:53:18.983: E/AndroidRuntime(1734): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
11-26 06:53:18.983: E/AndroidRuntime(1734): at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
11-26 06:53:18.983: E/AndroidRuntime(1734): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
11-26 06:53:18.983: E/AndroidRuntime(1734): at android.widget.AbsListView$1.run(AbsListView.java:3463)
11-26 06:53:18.983: E/AndroidRuntime(1734): at android.os.Handler.handleCallback(Handler.java:730)
11-26 06:53:18.983: E/AndroidRuntime(1734): at android.os.Handler.dispatchMessage(Handler.java:92)
11-26 06:53:18.983: E/AndroidRuntime(1734): at android.os.Looper.loop(Looper.java:137)
11-26 06:53:18.983: E/AndroidRuntime(1734): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-26 06:53:18.983: E/AndroidRuntime(1734): at java.lang.reflect.Method.invokeNative(Native Method)
11-26 06:53:18.983: E/AndroidRuntime(1734): at java.lang.reflect.Method.invoke(Method.java:525)
11-26 06:53:18.983: E/AndroidRuntime(1734): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-26 06:53:18.983: E/AndroidRuntime(1734): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-26 06:53:18.983: E/AndroidRuntime(1734): at dalvik.system.NativeStart.main(Native Method)

我的布局:

<!-- language: 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"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/anim_image"
android:layout_width="500sp"
android:layout_height="500sp"/>
</RelativeLayout>

最佳答案

您的问题从这里开始:

ImageView image1 = (ImageView)      dialog.findViewById(R.id.anim_image);

确保您的所有 findViewById() 方法都会返回您想要的内容。其中一些肯定会返回 null。

关于java - Android:将动画插入对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20216404/

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