单击按钮时无法执行活动的方法

时间:2014-07-06 13:51:04

标签: java android onclick android-logcat illegalstateexception

我正在开发一个Android应用程序。我有一个按钮,按下时会启动“getdata”功能。在log cat上它会抛出错误。 我认为“public int Day(int a)”方法存在一些问题。 我是Android应用程序开发的新手。请帮忙。

logcat的:

07-06 19:11:51.609: E/AndroidRuntime(5231): FATAL EXCEPTION: main
07-06 19:11:51.609: E/AndroidRuntime(5231): java.lang.IllegalStateException: Could not       execute method of the activity
07-06 19:11:51.609: E/AndroidRuntime(5231):     at   android.view.View$1.onClick(View.java:3680)
07-06 19:11:51.609: E/AndroidRuntime(5231):     at android.view.View.performClick(View.java:4191)
07-06 19:11:51.609: E/AndroidRuntime(5231):     at android.view.View$PerformClick.run(View.java:17229)
07-06 19:11:51.609: E/AndroidRuntime(5231):     at android.os.Handler.handleCallback(Handler.java:615)
07-06 19:11:51.609: E/AndroidRuntime(5231):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-06 19:11:51.609: E/AndroidRuntime(5231):     at android.os.Looper.loop(Looper.java:137)
07-06 19:11:51.609: E/AndroidRuntime(5231):     at android.app.ActivityThread.main(ActivityThread.java:4960)
07-06 19:11:51.609: E/AndroidRuntime(5231):     at java.lang.reflect.Method.invokeNative(Native Method)
07-06 19:11:51.609: E/AndroidRuntime(5231):     at java.lang.reflect.Method.invoke(Method.java:511)
07-06 19:11:51.609: E/AndroidRuntime(5231):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
07-06 19:11:51.609: E/AndroidRuntime(5231):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
07-06 19:11:51.609: E/AndroidRuntime(5231):     at dalvik.system.NativeStart.main(Native Method)
07-06 19:11:51.609: E/AndroidRuntime(5231): Caused by: java.lang.reflect.InvocationTargetException
07-06 19:11:51.609: E/AndroidRuntime(5231):     at java.lang.reflect.Method.invokeNative(Native Method)
07-06 19:11:51.609: E/AndroidRuntime(5231):     at java.lang.reflect.Method.invoke(Method.java:511)
07-06 19:11:51.609: E/AndroidRuntime(5231):     at android.view.View$1.onClick(View.java:3675)
07-06 19:11:51.609: E/AndroidRuntime(5231):     ... 11 more
07-06 19:11:51.609: E/AndroidRuntime(5231): Caused by: java.lang.NullPointerException
07-06 19:11:51.609: E/AndroidRuntime(5231):     at ishan.khandelwal.vitsilentmode.Labs.getdata(Labs.java:100)
07-06 19:11:51.609: E/AndroidRuntime(5231):     ... 14 more



public class Labs extends Activity {

//Initialise Calendars

private Calendar L[];

ArrayList <CheckBox> l= new ArrayList <CheckBox>(60);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_labs);
    addenable();

    //Initialise check boxes for every slot
    for (int i = 101; i <= 160; i++) {
           String viewId = "checkBox" + i;
           l.add((CheckBox) findViewById(getResources().getIdentifier(viewId, "id", getPackageName())));
        }       
}

我的代码:

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

public void addenable(){

}

public int Day(int a)
{
    int d=0;
    int x = a;
    x--;

    if(x>=30)
        d = (x/6)-3;

    else
        d=(x/6)+2;

    return d;
}

public int Hour(int a)
{
    int hr=0;
    int x=a;

    x--;

    hr=(x%6)+8;

    if(x>=4)
        hr--;

    if(x>=30)
        hr = hr+6;

    return hr;

}

public int Min(int a)
{
    int m=0;
    int x=a;

    if(x%6==5)
        m=50;

    if(x%6==0)
        m=40;

    return m;
}

public void getdata(View V){

    for( int i=0; i<60; i++)
    {
        L[i+1] = Calendar.getInstance();
        L[i+1].set(Calendar.DAY_OF_WEEK, Day(i+1));
        L[i+1].set(Calendar.HOUR_OF_DAY, Hour(i+1));
        L[i+1].set(Calendar.MINUTE, Min(i+1));
        L[i+1].set(Calendar.SECOND, 0);
        }

    //Intents
    Intent setVibration = new Intent();
    setVibration.setClass(this, AlarmReciever.class);

    Intent setNormal = new Intent();
    setNormal.setClass(this, RingerMode.class);

    //PENDING INTENTS
    ArrayList <PendingIntent> Lab_V= new ArrayList <PendingIntent>(60);
    ArrayList <PendingIntent> Lab_N= new ArrayList <PendingIntent>(60);

    for(int i=0; i<60; i++)
    {
        Lab_V.add(i, PendingIntent.getBroadcast(this, i, setVibration,
                PendingIntent.FLAG_UPDATE_CURRENT));
        Lab_N.add(i, PendingIntent.getBroadcast(this, i, setNormal,
                PendingIntent.FLAG_UPDATE_CURRENT));
    }

    // create the object
    AlarmManager mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    for(int i=0; i<60; i++)
    {
            if(l.get(i).isChecked()){

             mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, L[i].getTimeInMillis(), 7*24*60*60*1000, Lab_V.get(i));
             mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, L[i].getTimeInMillis()+50*60*1000, 7*24*60*60*1000, Lab_N.get(i));

            }
    }

    }
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

您正在访问一个空对象:

Caused by: java.lang.NullPointerException

创建数据时,您从1(i+1) not 0开始。

for( int i=0; i<60; i++)
{
    L[i+1] = Calendar.getInstance();
    L[i+1].set(Calendar.DAY_OF_WEEK, Day(i+1));
    L[i+1].set(Calendar.HOUR_OF_DAY, Hour(i+1));
    L[i+1].set(Calendar.MINUTE, Min(i+1));
    L[i+1].set(Calendar.SECOND, 0);
}

但是,稍后你试图读取索引0处的项目:

for(int i=0; i<60; i++)
{
        if(l.get(i).isChecked()){
             // ...
        }
}

l.get(0)将返回null并在空对象上调用isChecked()会导致NullPointerException