编译Java代码时出错

时间:2014-10-03 15:35:06

标签: java android debugging

我无法找到如何删除下面代码中的错误

package com.example.hellocodelearn;
import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.hellocodelearn.models.Notes;


 public class NotesListActivity extends ListActivity 

{

    private ArrayAdapter NotesItemArrayAdapter ;
    private List<Notes> notes = new ArrayList<Notes>() ; 

    for ( int i = 0; i < 20; i++ ) 
    {
        Notes tweet = new Notes();
        tweet.setTitle("A nice header for Tweet # " +i);
        tweet.setBody("Some random body text for the tweet # " +i);
        notes.add(tweet);
    }

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

      NotesItemArrayAdapter = new NotesAdapter(this, new String[10]);
      setListAdapter(NotesItemArrayAdapter);
 }

 @Override
 protected void onListItemClick(ListView l, View v, int position, long id) 
 {
     Intent intent = new Intent(this, NotesDetailActivity.class);
     startActivity(intent);
 }


}

我收到错误:

Description Resource    Path    Location    Type
Syntax error on token ",", ; expected   NotesListActivity.java  /HelloCodeLearn/src/com/example/hellocodelearn  line 47 Java Problem

请求帮助。谢谢。

2 个答案:

答案 0 :(得分:1)

你不能在你的课堂上有一个for循环,它必须进入一个方法。

答案 1 :(得分:1)

for循环应该在方法而不是在类块

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notes_list);
    for (int i = 0; i < 20; i++ ) {
      ....
    }
}