我在我的Fragment
课程中使用了这部分代码来显示wifi频道图表,问题是当我从第一个Fragment
更改为第二个Fragment
应用程序因崩溃而崩溃时Receiver
上的错误可以帮助我吗
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
WifiManager wifiMan=(WifiManager)getActivity().getSystemService(Context.WIFI_SERVICE);
wifiMan.startScan();
int length;
List<ScanResult> results = wifiManager.getScanResults();
if (intent.getAction().equals(
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
results = wifiManager.getScanResults();
length = results.size();
Log.i("length1", length + "");
XYMultipleSeriesRenderer renderer = getTruitonBarRenderer(results
.size());
myChartSettings(renderer);
chartLayout = (FrameLayout) getActivity().findViewById(
R.id.chart);
GraphicalView mChartView = ChartFactory.getBarChartView(
getActivity(), getTruitonBarDataset(results), renderer,
Type.STACKED);
if (mChartView != null) {
chartLayout.removeView(mChartView);
}
chartLayout.addView(mChartView, 0, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
}
}
这是我的logcat输出:
06-17 14:36:50.778: E/SpannableStringBuilder(10263): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-17 14:36:50.778: E/SpannableStringBuilder(10263): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-17 14:36:51.799: E/SpannableStringBuilder(10263): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-17 14:36:51.799: E/SpannableStringBuilder(10263): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-17 14:36:55.012: E/SpannableStringBuilder(10263): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-17 14:36:55.012: E/SpannableStringBuilder(10263): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-17 14:36:59.167: E/SpannableStringBuilder(10263): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-17 14:36:59.167: E/SpannableStringBuilder(10263): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
06-17 14:37:16.575: E/AndroidRuntime(10263): FATAL EXCEPTION: main
06-17 14:37:16.575: E/AndroidRuntime(10263): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS flg=0x8000010 } in com.esprit.testproject.apscan.GraphFragment$Receiver@423df448
06-17 14:37:16.575: E/AndroidRuntime(10263): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:765)
06-17 14:37:16.575: E/AndroidRuntime(10263): at android.os.Handler.handleCallback(Handler.java:615)
06-17 14:37:16.575: E/AndroidRuntime(10263): at android.os.Handler.dispatchMessage(Handler.java:92)
06-17 14:37:16.575: E/AndroidRuntime(10263): at android.os.Looper.loop(Looper.java:137)
06-17 14:37:16.575: E/AndroidRuntime(10263): at android.app.ActivityThread.main(ActivityThread.java:4867)
06-17 14:37:16.575: E/AndroidRuntime(10263): at java.lang.reflect.Method.invokeNative(Native Method)
06-17 14:37:16.575: E/AndroidRuntime(10263): at java.lang.reflect.Method.invoke(Method.java:511)
06-17 14:37:16.575: E/AndroidRuntime(10263): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
06-17 14:37:16.575: E/AndroidRuntime(10263): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
06-17 14:37:16.575: E/AndroidRuntime(10263): at dalvik.system.NativeStart.main(Native Method)
06-17 14:37:16.575: E/AndroidRuntime(10263): Caused by: java.lang.NullPointerException
06-17 14:37:16.575: E/AndroidRuntime(10263): at android.view.ViewConfiguration.get(ViewConfiguration.java:332)
06-17 14:37:16.575: E/AndroidRuntime(10263): at android.view.View.<init>(View.java:3304)
06-17 14:37:16.575: E/AndroidRuntime(10263): at org.achartengine.GraphicalView.<init>(GraphicalView.java:91)
06-17 14:37:16.575: E/AndroidRuntime(10263): at org.achartengine.ChartFactory.getBarChartView(ChartFactory.java:164)
06-17 14:37:16.575: E/AndroidRuntime(10263): at com.esprit.testproject.apscan.GraphFragment$Receiver.onReceive(GraphFragment.java:356)
06-17 14:37:16.575: E/AndroidRuntime(10263): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755)
06-17 14:37:16.575: E/AndroidRuntime(10263): ... 9 more
答案 0 :(得分:0)
您在NullPointerException
的通话中获得ChartFactory.getBarChartView
。因此,您可能想要检查您传递的参数之一是null
。
我怀疑在某些情况下,两个电话getTruitonBarRenderer()
和getTruitonBarDataset()
中的一个会返回null
。 (也许当WiFi扫描没有返回结果?)添加一些逻辑来处理这些情况,你应该很好。
但是,我现在也看到你以奇怪的方式处理mChartView
。
mChartView
声明为本地变量。示例代码将其作为类成员,因此其内容将在调用之间保留。chartLayout.removeView(mChartView)
之前调用mChartView
,从而在创建之前从布局中删除旧视图(在上次调用方法时创建)插入一个新的。在上面发布的代码中,对chartLayout.removeView(mChartView)
的调用将删除一个从未插入的布局,这可能会引发异常或者什么都不做。尝试替换这段代码
GraphicalView mChartView = ChartFactory.getBarChartView(
getActivity(), getTruitonBarDataset(results), renderer,
Type.STACKED);
if (mChartView != null) {
chartLayout.removeView(mChartView);
}
chartLayout.addView(mChartView, 0, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
与
XYMultipleSeriesDataset dataset = getTruitonBarDataset(results);
if ((dataset != null) && (renderer != null)) {
if (mChartView != null) {
chartLayout.removeView(mChartView);
}
GraphicalView mChartView = ChartFactory.getBarChartView(
getActivity(), dataset, renderer,
Type.STACKED);
chartLayout.addView(mChartView, 0, new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
并另外向您的班级添加mChartView
成员。