2 qq 28818163 qq_28818163 于 2015.06.10 22:02 提问

程序开始运行时强制关闭,Adapter方法出错(用来实现闹钟的时间列表)

package pcAlarm.pack;

import java.util.Calendar;
import java.util.Currency;

import pcAlarm.pack.PCAlarmActivity;

import android.app.Activity;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Contacts;
import android.view.View;
import android.widget.ArrayAdapter;
import pcAlarm.pack.AlarmView;
import pcAlarm.pack.AlarmView.AlarmData;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TabHost;
import android.widget.TimePicker;

public class PCAlarmActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();

    tabHost.addTab(tabHost.newTabSpec("tabAlarm").setIndicator("闹钟").setContent(R.id.tabAlarm));


    btnAddAlarm=(Button)findViewById(R.id.btnAddAlarm);
    lvListView=(ListView)findViewById(R.id.lvListView);

    adapter=new ArrayAdapter<PCAlarmActivity.AlarmData>(getContext(), android.R.layout.simple_list_item_1);
    lvListView.setAdapter(adapter);

    adapter.add(new AlarmData(System.currentTimeMillis()));





    btnAddAlarm.setOnClickListener(new OnClickListener() {

        private Calendar mcalender;

        @Override
        public void onClick(View v) {
            Calendar.getInstance().setTimeInMillis(System.currentTimeMillis());
            mcalender = Calendar.getInstance();
            int mHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
            int mMinute = Calendar.getInstance().get(Calendar.MINUTE);
            new TimePickerDialog(PCAlarmActivity.this, new TimePickerDialog.OnTimeSetListener() {

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    // TODO Auto-generated method stub
                    Calendar.getInstance().setTimeInMillis(System.currentTimeMillis());
                    Calendar.getInstance().set(Calendar.HOUR_OF_DAY, hourOfDay);
                    Calendar.getInstance().set(Calendar.MINUTE, minute);
                    Calendar.getInstance().set(Calendar.SECOND, 0);
                    Calendar.getInstance().set(Calendar.MILLISECOND, 0);

                    AlarmData add = new AlarmData(Calendar.getInstance().getTimeInMillis());




                }
            }, mHour, mMinute, true).show();
        }
    });

}
private Context getContext() {
    // TODO Auto-generated method stub
    return null;
}
private Button btnAddAlarm;
private ListView lvListView;
private ArrayAdapter<AlarmData> adapter;

private static class AlarmData{
    public AlarmData(long time){
        this.time=time;
        date= Calendar.getInstance();
        timeLabel=String.format("%d月%d日 %d:%d", 
                date.get(Calendar.MONTH)+1,
                date.get(Calendar.DAY_OF_MONTH),
                date.get(Calendar.HOUR_OF_DAY),
                date.get(Calendar.MINUTE));

    }

    public long getTime() {
        return time;
    }

    public String getTimeLabel() {
        return timeLabel;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return getTimeLabel();
    }

    private String timeLabel="";
    private long time=0;
    private Calendar date;
}

private TabHost tabHost;

}

5个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.06.10 23:08
已采纳
 adapter=new ArrayAdapter<PCAlarmActivity.AlarmData>(getContext(), android.R.layout.simple_list_item_1);
    lvListView.setAdapter(adapter);

    adapter.add(new AlarmData(System.currentTimeMillis()));//这种方式添加,会直接notify更新界面,但在oncreate中,界面没初始化完成。
        改成
        List<PCAlarmActivity.AlarmData> dataList = new ArrayList();
    dataList.add(new AlarmData(System.currentTimeMillis()));
    adapter=new ArrayAdapter<PCAlarmActivity.AlarmData>(getContext(), android.R.layout.simple_list_item_1,dataList);
    lvListView.setAdapter(adapter);试试吧,
qq_28818163
qq_28818163 可以了(高兴),谢谢!
2 年多之前 回复
danielinbiti
danielinbiti 回复qq_28818163:getContext()返回的是null,改成 adapter=new ArrayAdapter<PCAlarmActivity.AlarmData>(this, android.R.layout.simple_list_item_1);
2 年多之前 回复
qq_28818163
qq_28818163 调试到adapter=new ArrayAdapter<PCAlarmActivity.AlarmData>(getContext(), android.R.layout.simple_list_item_1);这一句是显示Sourse not found然后就不能前进了
2 年多之前 回复
danielinbiti
danielinbiti 回复qq_28818163: 单步调试一下,看看哪个是空指针呢,看看是不是findViewById没有找到对应的组件
2 年多之前 回复
qq_28818163
qq_28818163 java.lang.RuntimeException: Unable to start activity ComponentInfo{pcAlarm.pack/pcAlarm.pack.PCAlarmActivity}: java.lang.NullPointerException
2 年多之前 回复
qq_28818163
qq_28818163 可以加QQ说么?
2 年多之前 回复
qq_28818163
qq_28818163 threadid=1: thread exiting with uncaught exception (group=0x4001d800)
2 年多之前 回复
qq_28818163
qq_28818163 threadid=1: thread exiting with uncaught exception (group=0x4001d800)
2 年多之前 回复
danielinbiti
danielinbiti 回复qq_28818163: 那日志报什么错误呢
2 年多之前 回复
qq_28818163
qq_28818163 还是一样的问题,没有改变
2 年多之前 回复
qq_28818163
qq_28818163   2015.06.10 23:31

还是一样的问题,没有改变。

qq_28818163
qq_28818163   2015.06.10 23:39

还可以用其它方法实现闹钟列表也可以,我是新手,可以尽量说详细点么?

u011133213
u011133213   2015.06.11 09:18

如果还没有解决,你可以联系我。我帮你看下

qq_28818163
qq_28818163 你的QQ?或者你加我的QQ1034052057。谢啦!!
2 年多之前 回复
windsunmoon
windsunmoon   Rxr 2015.06.11 12:29

空指针异常啊。看看是哪一行 null

Csdn user default icon
上传中...
上传图片
插入图片