-1

我尝试将 ListView 创建为设置页面。第一行是 EditText;2-5 行是两个文本。

所以我的适配器类:

public class SettingsAdapter extends BaseAdapter {
    private static final int TYPE_NAME = 0;
    private static final int TYPE_SETTING = 1;
    private static final int TYPE_MAX_COUNT = 2;

    SPMEditActionActivity   context;
    LayoutInflater          inflater;
    ArrayList               data;


    SettingsAdapter(SPMEditActionActivity _context) {

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

        data = new ArrayList();

        for(int i=0;i<5;i++)
            data.add("Hello");
    }



    @Override
    public int getItemViewType(int position) {
        return position==0 ? TYPE_NAME : TYPE_SETTING;
    }

    @Override
    public int getViewTypeCount() {
        return TYPE_MAX_COUNT;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    // пункт списка
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int type = getItemViewType(position);


        View view = convertView;
        if (view == null) {
            if(type == TYPE_NAME)
                view = inflater.inflate(R.layout.editactionactivity_settings_name, parent, false);
            else
                view = inflater.inflate(R.layout.editactionactivity_setting, parent, false);

        }
        return view;
    }

}

活动:

public class SPMEditActionActivity extends Activity {


    final int START_TIME_DIALOG = 1;
    final int END_TIME_DIALOG = 2;

//==============UI===============================================
    private ListView        settings_listview ; 
    private SettingsAdapter settings_adapter;

    private Button save_button;
    private Button cancel_button;
//==============================================================

//========UI LISTENERS==========================================


        private OnClickListener save_button_listener = new OnClickListener() {
            public void onClick(View v) {
                 save();
            }
        };      
        private OnClickListener cancel_button_listener = new OnClickListener() {
            public void onClick(View v) {
                 cancel();
            }
        };

//==============================================================

//==============================================================

//=================LIFECYCLE METHODS============================
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spmeditaction);



        save_button  = (Button) findViewById(R.id.EditActionActivity_save_button);
        cancel_button= (Button) findViewById(R.id.EditActionActivity_cancel_button);

        settings_adapter = new SettingsAdapter(SPMEditActionActivity.this);
        settings_listview.setAdapter(  settings_adapter    );


        save_button.setOnClickListener(save_button_listener);
        cancel_button.setOnClickListener(cancel_button_listener);


    }

//==============================================================
    private void save(){
        finish();

    }

    private void cancel(){
        finish();
    }

}

在活动中,当我设置适配器时

settings_adapter = new SettingsAdapter(this);
settings_listview.setAdapter(  settings_adapter    );

我的应用程序因 NullPointerException 而崩溃。

谁能向我解释,我做错了什么?

PS。我用这个教程

日志输出:

04-18 15:59:19.982: E/AndroidRuntime(24150): FATAL EXCEPTION: main
04-18 15:59:19.982: E/AndroidRuntime(24150): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hemul.soundprofilemanager/com.hemul.soundprofilemanager.SPMEditActionActivity}: java.lang.NullPointerException
04-18 15:59:19.982: E/AndroidRuntime(24150):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-18 15:59:19.982: E/AndroidRuntime(24150):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-18 15:59:19.982: E/AndroidRuntime(24150):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-18 15:59:19.982: E/AndroidRuntime(24150):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-18 15:59:19.982: E/AndroidRuntime(24150):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 15:59:19.982: E/AndroidRuntime(24150):    at android.os.Looper.loop(Looper.java:137)
04-18 15:59:19.982:E/AndroidRuntime(24150):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-18 15:59:19.982: E/AndroidRuntime(24150):    at java.lang.reflect.Method.invokeNative(Native Method)
04-18 15:59:19.982: E/AndroidRuntime(24150):    at java.lang.reflect.Method.invoke(Method.java:511)
04-18 15:59:19.982: E/AndroidRuntime(24150):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-18 15:59:19.982: E/AndroidRuntime(24150):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-18 15:59:19.982: E/AndroidRuntime(24150):    at dalvik.system.NativeStart.main(Native Method)
04-18 15:59:19.982: E/AndroidRuntime(24150): Caused by: java.lang.NullPointerException
04-18 15:59:19.982: E/AndroidRuntime(24150):    at com.hemul.soundprofilemanager.SPMEditActionActivity.onCreate(SPMEditActionActivity.java:111)
04-18 15:59:19.982: E/AndroidRuntime(24150):    at android.app.Activity.performCreate(Activity.java:5008)
04-18 15:59:19.982: E/AndroidRuntime(24150):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-18 15:59:19.982: E/AndroidRuntime(24150):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-18 15:59:19.982: E/AndroidRuntime(24150):    ... 11 more
4

1 回答 1

1

settings_listview是空的,因为你没有在 onCreate 中分配对 this 的引用

像这样从 xml 获取您的列表视图参考

settings_listview=(ListView)findViewById(R.id.yourlistview);  <-- your listview id in xml

setContentView(R.layout.activity_spmeditaction);

于 2013-04-18T12:12:21.680 回答