不幸的是,(App Name)已停止

时间:2014-01-23 17:23:30

标签: android android-layout android-intent

我能够成功编译,构建和安装项目。当我点击一个按钮时,该应用程序说:不幸的是已停止。你能帮我解决这个问题吗?

01-23 20:51:58.672: W/dalvikvm(1946): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
01-23 20:51:58.722: E/AndroidRuntime(1946): FATAL EXCEPTION: main
01-23 20:51:58.722: E/AndroidRuntime(1946): java.lang.NullPointerException
01-23 20:51:58.722: E/AndroidRuntime(1946):     at com.Divani.Marzieh.CustomlistActivity.getView(CustomlistActivity.java:32)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.widget.AbsListView.obtainView(AbsListView.java:2033)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.widget.ListView.onMeasure(ListView.java:1155)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.view.View.measure(View.java:12723)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.view.View.measure(View.java:12723)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.view.View.measure(View.java:12723)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.view.View.measure(View.java:12723)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.view.View.measure(View.java:12723)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.view.View.measure(View.java:12723)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.view.View.measure(View.java:12723)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.os.Looper.loop(Looper.java:137)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at android.app.ActivityThread.main(ActivityThread.java:4424)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at java.lang.reflect.Method.invokeNative(Native Method)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at java.lang.reflect.Method.invoke(Method.java:511)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-23 20:51:58.722: E/AndroidRuntime(1946):     at dalvik.system.NativeStart.main(Native Method)

这是CustomListActivity代码:

class CustomlistActivity extends ArrayAdapter<Item>{
    ArrayList<Item> strings;
    public CustomlistActivity( Context context, int resource, int textViewResourceId, ArrayList<Item> string) {
        super(context, resource, textViewResourceId, string);
            strings = string; 
    }
    @Override
    public View getView(int position,View convertView,ViewGroup parent){
        LayoutInflater inflater  = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.list_item, parent ,false);
        ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
        TextView tv = (TextView) row.findViewById(R.id.textView1);
        Item item = strings.get(position);
        iv.setImageResource(item.image);
        tv.setText(item.name);
        return row;
    }
    private LayoutInflater getSystemService(String layoutInflaterService) {
        return null;
    }
}

1 个答案:

答案 0 :(得分:0)

http://developer.android.com/reference/android/content/Context.html#getSystemService(java.lang.String)

getSystemService需要一个上下文。

所以改变你的代码如下

Context context;
public CustomlistActivity( Context context, int resource, int textViewResourceId,
            ArrayList<Item> string) {
        super(context, resource, textViewResourceId, string);
        // TODO Auto-generated constructor stub
        strings = string; 
        this.context = context;
    }

然后

LayoutInflater inflater  = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

并删除

 private LayoutInflater getSystemService(String layoutInflaterService) {
    // TODO Auto-generated method stub
    return null;
    }

另请考虑使用ViewHolder模式

http://developer.android.com/training/improving-layouts/smooth-scrolling.html