gpt4 book ai didi

java - Android App 中的 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 07:50:30 24 4
gpt4 key购买 nike

我正在尝试创建一个应用程序,它将使用 AndroidPlot 库绘制存储在 raw 中的 WAV 文件的波形。然后,首先我要做的是获取波形的字节数组。我猜它是以 2 个字节为对存储的,我尝试获取它们中的每一个的振幅,并将其存储在一个 int 数组中,该数组稍后将转换为一个 Number 数组。我现在只想尝试字节数组的前 100 个样本。这是我到目前为止的代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Number[] n = new Number[51];
int[] array = new int[51];

try {
byte [] payload = IOUtils.toByteArray(this.getResources().openRawResource(R.raw.radio));
for (int i = 0; i <= 100; i+=2) {
// convert byte pair to int
int j=i/2;
int audioSample = (int) ((payload[i+1] & 0xff) << 8) | (payload[i] & 0xff)/ 32767;
array[j]=audioSample;
n[j] = (Number)array[j];
j++;
}
} catch (NotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

XYSeries series1 = new SimpleXYSeries(
Arrays.asList(n), // SimpleXYSeries takes a List so turn our array into a List
SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, // Y_VALS_ONLY means use the element index as the x value
"Series1");

LineAndPointFormatter series1Format = new LineAndPointFormatter(Color.RED, Color.GREEN, Color.BLUE, null);


// add a new series' to the xyplot:
plot.addSeries(series1, series1Format);

// reduce the number of range labels
plot.setTicksPerRangeLabel(3);
plot.getGraphWidget().setDomainLabelOrientation(-45);


}

当我尝试编译代码时出现 NullPointerException 错误。我猜是 tha 数组出了问题。请你帮助我好吗?我对 Java 很陌生

02-06 18:45:30.946: E/AndroidRuntime(2642): FATAL EXCEPTION: main
02-06 18:45:30.946: E/AndroidRuntime(2642): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.representaaudio/com.example.representaaudio.MainActivity}: java.lang.NullPointerException
02-06 18:45:30.946: E/AndroidRuntime(2642): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-06 18:45:30.946: E/AndroidRuntime(2642): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-06 18:45:30.946: E/AndroidRuntime(2642): at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-06 18:45:30.946: E/AndroidRuntime(2642): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-06 18:45:30.946: E/AndroidRuntime(2642): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 18:45:30.946: E/AndroidRuntime(2642): at android.os.Looper.loop(Looper.java:137)
02-06 18:45:30.946: E/AndroidRuntime(2642): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-06 18:45:30.946: E/AndroidRuntime(2642): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 18:45:30.946: E/AndroidRuntime(2642): at java.lang.reflect.Method.invoke(Method.java:511)
02-06 18:45:30.946: E/AndroidRuntime(2642): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-06 18:45:30.946: E/AndroidRuntime(2642): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-06 18:45:30.946: E/AndroidRuntime(2642): at dalvik.system.NativeStart.main(Native Method)
02-06 18:45:30.946: E/AndroidRuntime(2642): Caused by: java.lang.NullPointerException
02-06 18:45:30.946: E/AndroidRuntime(2642): at android.util.TypedValue.applyDimension(TypedValue.java:327)
02-06 18:45:30.946: E/AndroidRuntime(2642): at com.androidplot.util.PixelUtils.dpToPix(PixelUtils.java:103)
02-06 18:45:30.946: E/AndroidRuntime(2642): at com.androidplot.xy.LineAndPointFormatter.a(LineAndPointFormatter.java:101)
02-06 18:45:30.946: E/AndroidRuntime(2642): at com.androidplot.xy.LineAndPointFormatter.<init>(LineAndPointFormatter.java:63)
02-06 18:45:30.946: E/AndroidRuntime(2642): at com.androidplot.xy.LineAndPointFormatter.<init>(LineAndPointFormatter.java:74)
02-06 18:45:30.946: E/AndroidRuntime(2642): at com.example.representaaudio.MainActivity.onCreate(MainActivity.java:76)
02-06 18:45:30.946: E/AndroidRuntime(2642): at android.app.Activity.performCreate(Activity.java:5104)
02-06 18:45:30.946: E/AndroidRuntime(2642): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-06 18:45:30.946: E/AndroidRuntime(2642): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-06 18:45:30.946: E/AndroidRuntime(2642): ... 11 more

最佳答案

如果你正在使用 Plotstuff pixelUtils,你应该先调用 init 方法

 /**
* Recalculates scale value etc. Should be called when an application starts or
* whenever the screen is rotated.
*/
public static void init(Context ctx) {
//DisplayMetrics dm = ctx.getResources().getDisplayMetrics();
//SCALE = dm.density;
//X_PIX = dm.widthPixels;
//Y_PIX = dm.heightPixels;
metrics = ctx.getResources().getDisplayMetrics();

}

关于java - Android App 中的 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21612393/

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