gpt4 book ai didi

android - 将微调器添加到应用程序,然后崩溃

转载 作者:行者123 更新时间:2023-11-30 00:12:43 25 4
gpt4 key购买 nike

我在我的应用程序中添加了一个微调器,但应用程序崩溃了,无法构建。微调器显示存储中的 .mp3 文件名项目。有人能说出崩溃的原因吗?我的代码有什么问题?如果您需要更多代码,请告诉我。

这是MainActivity的一部分

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//import android.view.Window;



public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {

private Integer images[]={R.drawable.pic1, R.drawable.pic21,
R.drawable.pic3, R.drawable.pic2, R.drawable.pic4,
R.drawable.pic5, R.drawable.pic6, R.drawable.pic30,
R.drawable.pic31, R.drawable.pic26, R.drawable.pic7, R.drawable.pic25,
R.drawable.pic8, R.drawable.pic9,
R.drawable.pic10, R.drawable.pic27, R.drawable.pic28,
R.drawable.pic29, R.drawable.pic11, R.drawable.pic22, R.drawable.pic23,
R.drawable.pic13, R.drawable.pic14,
R.drawable.pic15, R.drawable.pic16, R.drawable.pic24,
R.drawable.pic17, R.drawable.pic18, R.drawable.pic19
, R.drawable.pic20, R.drawable.pic12};
.........
.........
// for spinner
class Mp3Filter implements FilenameFilter {
public boolean accept (File dir, String name){
return (name.endsWith(".mp3"));
}
}
// spinner
public final String SD_PATH =new String("/sdcard/music/");
public List<String> songs = new ArrayList<String>();
Spinner mySpinner = (Spinner)findViewById(R.id.spinner1);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.....
.....
mp = MediaPlayer.create(this, R.raw.shlomoimagine);
mp.start();
// currImage = 0;
setContentView(R.layout.activity_main);


//initializing the loop at the beginning of the app run
initializeImageSwitcher();
setImageRotateListener();
// handler.post(runnable);
updatePlayList();

...
...
private void updatePlayList() {
File home=new File(SD_PATH);

File[] files = home.listFiles(new Mp3Filter());
if(files.length>0){
for(File file:files){
songs.add(file.getName());
}
ArrayAdapter<String> songList = new ArrayAdapter<String>
(this,R.layout.song_item,songs);
mySpinner.setAdapter(songList);
mySpinner.setOnItemSelectedListener(this);

}
}

这些是 list 中的部分

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nati_1.ImageGallerySwitching_v8_2">

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.NoActionBar">

歌曲项目.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"
android:textStyle="bold"
/>

来自 logcat

 12-20 00:22:48.014 12887-12887/com.nati_1.ImageGallerySwitching_v6 
E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.nati_1.ImageGallerySwitching_v6, PID: 12887



java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.nati_1.ImageGallerySwitching_v6/com.nati_1.ImageGallerySwitching_v8_2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2614)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2787)

at android.app.ActivityThread.-wrap12(ActivityThread.java)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504)

at android.os.Handler.dispatchMessage(Handler.java:102)

at android.os.Looper.loop(Looper.java:154)

at android.app.ActivityThread.main(ActivityThread.java:6247)

at java.lang.reflect.Method.invoke(Native Method)

at


com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
'android.view.Window$Callback android.view.Window.getCallback()' on a null
object reference

at android.support.v7.app.AppCompatDelegateImplBase.<init>
(AppCompatDelegateImplBase.java:116)


at android.support.v7.app.AppCompatDelegateImplV9.<init>
(AppCompatDelegateImplV9.java:147)

at android.support.v7.app.AppCompatDelegateImplV11.<init>
(AppCompatDelegateImplV11.java:27)


at android.support.v7.app.AppCompatDelegateImplV14.<init>
(AppCompatDelegateImplV14.java:50)

at android.support.v7.app.AppCompatDelegateImplV23.<init>
(AppCompatDelegateImplV23.java:29)

at android.support.v7.app.AppCompatDelegateImplN.<init>
(AppCompatDelegateImplN.java:29)

at
android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:197)

at
android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:181)

at
android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:521)

at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.nati_1.ImageGallerySwitching_v8_2.MainActivity.<init>(MainActivity.java:225)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2604)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2787) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6247) 
at java.lang.reflect.Method.invoke(Native Method) 

最佳答案

问题定义为:

java.lang.NullPointerException: Attempt to invoke virtual method 
'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

问题是您正在为 Spinner 创建一个变量并同时获取它们的引用:

Spinner mySpinner = (Spinner)findViewById(R.id.spinner1);

但这是不正确的,您必须在 onCreate() 方法中获取引用,有些像这样:

Spinner mySpinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

mySpinner = (Spinner)findViewById(R.id.spinner1);
...
...
}

关于android - 将微调器添加到应用程序,然后崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47896197/

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