这是我为ListView
创建自定义视图的代码。它使用名为SquareProgressBar
的自定义视图:
package com.example.simpledownloader.adapter;
import net.yscs.android.square_progressbar.SquareProgressBar;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.simpledownloader.R;
import com.example.simpledownloader.sharable.Sharable;
public class TaskAdapter extends BaseAdapter {
Context ctx = null;
public TaskAdapter(Context ctx){
this.ctx = ctx;
}
//--------------------------------------------------------------------------------
@Override
public int getCount() {
return Sharable.downloads.size();
}
//--------------------------------------------------------------------------------
@Override
public Object getItem(int index) {
return Sharable.downloads.get(index);
}
//--------------------------------------------------------------------------------
@Override
public long getItemId(int id) {
return id;
}
//--------------------------------------------------------------------------------
@Override
public View getView(int index, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater inflater = LayoutInflater.from(ctx);
convertView = inflater.inflate(R.layout.task, parent, false);
}
SquareProgressBar taskProgressBar = (SquareProgressBar) convertView.findViewById(R.id.taskProgressBar);
TextView statusMsg = (TextView) convertView.findViewById(R.id.statusMsg);
TextView name = (TextView) convertView.findViewById(R.id.taskName);
taskProgressBar.setProgress((int)Sharable.downloads.get(index).getProgress());
taskProgressBar.setImage(R.drawable.task_image);
taskProgressBar.setColor("#C9C9C9");
taskProgressBar.setWidth(8);
name.setText(Sharable.downloads.get(index).toString());
statusMsg.setText(Sharable.downloads.get(index).getStatus());
return convertView;
}
//--------------------------------------------------------------------------------
}
但是,我得到了一个NPE:
08-18 15:10:56.184: E/AndroidRuntime(5639): FATAL EXCEPTION: main
08-18 15:10:56.184: E/AndroidRuntime(5639): java.lang.NullPointerException
08-18 15:10:56.184: E/AndroidRuntime(5639): at net.yscs.android.square_progressbar.SquareProgressBar.setOpacity(SquareProgressBar.java:184)
08-18 15:10:56.184: E/AndroidRuntime(5639): at net.yscs.android.square_progressbar.SquareProgressBar.setProgress(SquareProgressBar.java:105)
08-18 15:10:56.184: E/AndroidRuntime(5639): at com.example.simpledownloader.adapter.TaskAdapter.getView(TaskAdapter.java:47)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.widget.ListView.onMeasure(ListView.java:1158)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.View.measure(View.java:15518)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.View.measure(View.java:15518)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.View.measure(View.java:15518)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.View.measure(View.java:15518)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-18 15:10:56.184: E/AndroidRuntime(5639): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.View.measure(View.java:15518)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.Choreographer.doCallbacks(Choreographer.java:562)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.Choreographer.doFrame(Choreographer.java:532)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.os.Handler.handleCallback(Handler.java:725)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.os.Handler.dispatchMessage(Handler.java:92)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.os.Looper.loop(Looper.java:137)
08-18 15:10:56.184: E/AndroidRuntime(5639): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-18 15:10:56.184: E/AndroidRuntime(5639): at java.lang.reflect.Method.invokeNative(Native Method)
08-18 15:10:56.184: E/AndroidRuntime(5639): at java.lang.reflect.Method.invoke(Method.java:511)
08-18 15:10:56.184: E/AndroidRuntime(5639): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-18 15:10:56.184: E/AndroidRuntime(5639): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-18 15:10:56.184: E/AndroidRuntime(5639): at dalvik.system.NativeStart.main(Native Method)
为什么?
我想是找不到方形进度条。
ctx是传递给这个类的应用程序上下文
sharable是一个自定义类,它包含在活动之间共享的所有单例对象。
最佳答案
而不是:
taskProgressBar.setProgress((int)Sharable.downloads.get(index).getProgress());
taskProgressBar.setImage(R.drawable.task_image);
使用:
taskProgressBar.setImage(R.drawable.task_image);
taskProgressBar.setProgress((int)Sharable.downloads.get(index).getProgress());
关于android - 实例化convertView时的NPE,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18300576/