简体   繁体   English

编译Java代码时出错

[英]Error in Compiling Java code

I cant find how to remove the errors in the code below 我在下面的代码中找不到如何删除错误

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);
 }


}

I get errors : 我收到错误消息:

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

Pleas help.Thanks. 请帮助。谢谢。

您的类主体中不能有for循环,它必须放在方法内部。

The for loop should be in a method rather than in the class block for循环应该在方法中,而不是在类块中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM