0

我正在尝试使用此代码,这是从此处获取的示例https://gist.github.com/2383248,但它在 public void onClick 上出现错误,即

此行的多个标记 - 实现 android.view.View.OnClickListener.onClick - 语法错误,插入“}”以完成 MethodBody,但是当我添加大括号时,它只是在多次尝试和不同建议和想法失败后引发另一个错误。

这可能是我的语法错误和糟糕的编码(刚开始学习编程),但有没有人知道如何解决这个问题或指出我正确的方向,我将不胜感激。

 public class ICSCalendarActivity extends Activity implements View.OnClickListener{

Button button1;

int year1;  
int month1;  
int day1;
int ShiftPattern;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    button1 = (Button)findViewById(R.id.openButton);     
    button1.setText("open");   
    button1.setOnClickListener(this);

    Bundle extras = getIntent().getExtras(); 

    year1 = extras.getInt("year1"); 
    day1 = extras.getInt("day1"); 
    month1 = extras.getInt("month1");

    ShiftPattern = extras.getInt("ShiftPattern");
}

public void onClick(View v){

private static void addToCalendar(Context ICSCalendarActivity, final String title, final long dtstart, final long dtend) {
    final ContentResolver cr = ICSCalendarActivity.getContentResolver();
    Cursor cursor ;
    if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
        cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    else
        cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    if ( cursor.moveToFirst() ) {
        final String[] calNames = new String[cursor.getCount()];
        final int[] calIds = new int[cursor.getCount()];
        for (int i = 0; i < calNames.length; i++) {
            calIds[i] = cursor.getInt(0);
            calNames[i] = cursor.getString(1);
            cursor.moveToNext();
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(ICSCalendarActivity);
        builder.setSingleChoiceItems(calNames, -1, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                ContentValues cv = new ContentValues();
                cv.put("calendar_id", calIds[which]);
                cv.put("title", title);
                cv.put("dtstart", dtstart );
                cv.put("hasAlarm", 1);
                cv.put("dtend", dtend);

                Uri newEvent ;
                if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
                    newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv);
                else
                    newEvent = cr.insert(Uri.parse("content://calendar/events"), cv);

                if (newEvent != null) {
                    long id = Long.parseLong( newEvent.getLastPathSegment() );
                    ContentValues values = new ContentValues();
                    values.put( "event_id", id );
                    values.put( "method", 1 );
                    values.put( "minutes", 15 ); // 15 minutes
                    if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
                        cr.insert( Uri.parse( "content://com.android.calendar/reminders" ), values );
                    else
                        cr.insert( Uri.parse( "content://calendar/reminders" ), values );

                }
                dialog.cancel();
            }

        });

        builder.create().show();
    }
    cursor.close();
}    }

谢谢你。

4

2 回答 2

2

函数内部不能有static方法定义。addToCalendaronClick

public class ICSCalendarActivity extends Activity implements View.OnClickListener{

Button button1;

int year1;  
int month1;  
int day1;
int ShiftPattern;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    button1 = (Button)findViewById(R.id.openButton);     
    button1.setText("open");   
    button1.setOnClickListener(this);

    Bundle extras = getIntent().getExtras(); 

    year1 = extras.getInt("year1"); 
    day1 = extras.getInt("day1"); 
    month1 = extras.getInt("month1");

    ShiftPattern = extras.getInt("ShiftPattern");
}

public void onClick(View v){
      addToCalendar(mContext, mString, mlong,  mlong); // pass the parameters of the addToCalendar method here 
      }

private static void addToCalendar(Context ICSCalendarActivity, final String title, final long dtstart, final long dtend) {
    final ContentResolver cr = ICSCalendarActivity.getContentResolver();
    Cursor cursor ;
    if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
        cursor = cr.query(Uri.parse("content://com.android.calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    else
        cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
    if ( cursor.moveToFirst() ) {
        final String[] calNames = new String[cursor.getCount()];
        final int[] calIds = new int[cursor.getCount()];
        for (int i = 0; i < calNames.length; i++) {
            calIds[i] = cursor.getInt(0);
            calNames[i] = cursor.getString(1);
            cursor.moveToNext();
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(ICSCalendarActivity);
        builder.setSingleChoiceItems(calNames, -1, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                ContentValues cv = new ContentValues();
                cv.put("calendar_id", calIds[which]);
                cv.put("title", title);
                cv.put("dtstart", dtstart );
                cv.put("hasAlarm", 1);
                cv.put("dtend", dtend);

                Uri newEvent ;
                if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
                    newEvent = cr.insert(Uri.parse("content://com.android.calendar/events"), cv);
                else
                    newEvent = cr.insert(Uri.parse("content://calendar/events"), cv);

                if (newEvent != null) {
                    long id = Long.parseLong( newEvent.getLastPathSegment() );
                    ContentValues values = new ContentValues();
                    values.put( "event_id", id );
                    values.put( "method", 1 );
                    values.put( "minutes", 15 ); // 15 minutes
                    if (Integer.parseInt(Build.VERSION.SDK) >= 8 )
                        cr.insert( Uri.parse( "content://com.android.calendar/reminders" ), values );
                    else
                        cr.insert( Uri.parse( "content://calendar/reminders" ), values );

                }
                dialog.cancel();
            }

        });

        builder.create().show();
    }
    cursor.close();
}  
于 2012-09-09T16:29:22.903 回答
0

您不能在另一个方法中定义一个方法。在您的代码中,您在 onClick() 方法中定义了 addToCalendar( ) 方法。而是在外部定义方法并从 onClick() 方法调用它。

public void onClick(View v)
{
     addTocalender(context, string, longVal1,  longVal2);
}

private static void addToCalendar(Context ICSCalendarActivity,
        final String title, final long dtstart, final long dtend)
{
 ....
 ....
}
于 2012-09-09T17:01:24.487 回答