gpt4 book ai didi

java - 未知的 IllegalArgumentException 文本不能为 null

转载 作者:行者123 更新时间:2023-12-02 06:49:35 25 4
gpt4 key购买 nike

我在一项 Activity 中收到了奇怪的 IAE。

08-13 16:24:59.991: E/AndroidRuntime(924): FATAL EXCEPTION: main
08-13 16:24:59.991: E/AndroidRuntime(924): java.lang.IllegalArgumentException: text cannot be null
08-13 16:24:59.991: E/AndroidRuntime(924): at android.graphics.Paint.measureText(Paint.java:1281)
08-13 16:24:59.991: E/AndroidRuntime(924): at com.echo.holographlibrary.BarGraph.onDraw(BarGraph.java:151)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.View.draw(View.java:10880)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.drawChild(ViewGroup.java:2899)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2495)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.drawChild(ViewGroup.java:2897)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2495)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.View.draw(View.java:10883)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.widget.FrameLayout.draw(FrameLayout.java:450)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.widget.ScrollView.draw(ScrollView.java:1524)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.drawChild(ViewGroup.java:2899)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2495)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.View.draw(View.java:10883)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.widget.FrameLayout.draw(FrameLayout.java:450)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.drawChild(ViewGroup.java:2899)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2495)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.drawChild(ViewGroup.java:2897)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2495)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.View.draw(View.java:10883)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.widget.FrameLayout.draw(FrameLayout.java:450)
08-13 16:24:59.991: E/AndroidRuntime(924): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2106)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2005)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1613)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2418)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.os.Looper.loop(Looper.java:137)
08-13 16:24:59.991: E/AndroidRuntime(924): at android.app.ActivityThread.main(ActivityThread.java:4340)
08-13 16:24:59.991: E/AndroidRuntime(924): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 16:24:59.991: E/AndroidRuntime(924): at java.lang.reflect.Method.invoke(Method.java:511)
08-13 16:24:59.991: E/AndroidRuntime(924): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-13 16:24:59.991: E/AndroidRuntime(924): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-13 16:24:59.991: E/AndroidRuntime(924): at dalvik.system.NativeStart.main(Native Method)
08-13 16:29:41.041: I/Process(924): Sending signal. PID: 924 SIG: 9

这之前工作得很好,但现在我使用了一些动态添加到 xml 的方法,它似乎崩溃了。以下是调用,我一直无法捕获此异常。

package com.example.exercise;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.Menu;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.echo.holographlibrary.Bar;
import com.echo.holographlibrary.BarGraph;

public class GraphDetail extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graph_detail);
try{
boolean done = false;
int begin = 0;
int count = 4;
while(!done)
{
if(count>=MainActivity.test.size())
{
done = true;
break;
}
ArrayList<Bar> points = new ArrayList<Bar>();
Intent intent1 = getIntent();
int max = (intent1.getIntExtra("max", 0));
int[] weight = new int[MainActivity.test.size()];
for(int i=begin; i<count; i++)
{
weight[i] = MainActivity.test.get(i).getWeight();
}
String[] names = new String[MainActivity.test.size()];
for(int i = begin; i<count; i++)
{
names[i] = MainActivity.test.get(i).getName();
}

for(int i = 0; i< weight.length; i++)
{
Bar d = new Bar();
if(weight[i]!=max)
d.setColor(Color.parseColor("#99CC00"));
else
{
d.setColor(Color.parseColor("#FFBB33"));
}

d.setName(names[i]);
d.setValue(weight[i]);
points.add(d);
}
Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 130, r.getDisplayMetrics());
RelativeLayout rl = (RelativeLayout)findViewById(R.id.container);
int px1 = (int)px;
BarGraph bar = new BarGraph(this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
px1);
bar.setBars(points);

bar.setUnit("0");
bar.appendUnit(true);
rl.addView(bar, lp);

begin+=count;
count+=3;
if(count>=MainActivity.test.size())
{
done = true;
break;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}

// ArrayList<Bar> points = new ArrayList<Bar>();
// ArrayList<Bar> points1 = new ArrayList<Bar>();
// ArrayList<Bar> points2 = new ArrayList<Bar>();
// Intent intent1 = getIntent();
// int max = (intent1.getIntExtra("max",0));
// int[] weight = new int[MainActivity.test.size()];
// for(int i = 0; i<MainActivity.test.size(); i++)
// {
// weight[i] = MainActivity.test.get(i).getWeight();
// }
// String[] names = new String[MainActivity.test.size()];
// for(int i = 0; i<MainActivity.test.size(); i++)
// {
// names[i] = MainActivity.test.get(i).getName();
// }
// for(int i = 0; i< weight.length; i++)
// {
// Bar d = new Bar();
// if(weight[i]!=max)
// d.setColor(Color.parseColor("#99CC00"));
// else
// {
// d.setColor(Color.parseColor("#FFBB33"));
// }
//
// d.setName(names[i]);
// d.setValue(weight[i]);
// if(i<4)
// points.add(d);
// if(i>=4 && i<8)
// points1.add(d);
// if(i>=8)
// points2.add(d);
// }
// BarGraph g = (BarGraph) findViewById(R.id.graph);
// g.setBars(points);
// BarGraph g1 = (BarGraph) findViewById(R.id.graph1);
// g1.setBars(points1);
// BarGraph g2 = (BarGraph) findViewById(R.id.graph2);
// g2.setBars(points2);
// g.setUnit("0");
// g.appendUnit(true);
// g1.setUnit("0");
// g1.appendUnit(true);
// g2.setUnit("0");
// g2.appendUnit(true);
//


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.graph_detail, menu);
return true;
}

}

最佳答案

String[] names = new String[MainActivity.test.size()];
for(int i = begin; i<count; i++)
{
names[i] = MainActivity.test.get(i).getName();
}
...
d.setName(names[i]);
...
bar.setBars(points);

您检查过名称数组的长度与计数吗?您生成的数组可能大于 count 的大小,因此您稍后将 null 传递给 d.setName,这最终会在稍后调用 bar.setBars 时导致异常。

关于java - 未知的 IllegalArgumentException 文本不能为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18214751/

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