0

With the caveat I am fairly new to android programing I was having some trouble with an string array declaration along these lines

String[] title = {
"Abundance",
"Anxiety",
"Breakups",
"Bruxism"};

String[] name = {
"test1",
"test2",
"test3",
"test4"};

The problem arose when I made this change

String[] title = {
"Abundance",
"Anxiety",
"Breakups",
"Bruxism"};

String urlbase = "http://www.somewhere.com/data/";
String imgSel = "/logo.png";
String[] mStrings = new String[title.length];

for(int i=0;i<title.length;i++) {
    mStrings[i] = urlbase + title[i].toLowerCase() + imgSel;
    System.out.println(mStrings[i]);
}

and then on the System.out line I saw this error

Syntax error on token ";", { expected after this token

which confused me and other for a long time until I found something I had no method, all of the previous statements were made in the class declaration.

***learned something here

I saw this in another post and tried their solution which was to add this

public static void main(String[] args) {


}

around my 'actions' and this looked like the right thing and made sense so I tried it. When I did it eclipse blew a screw and no gives me something really weird I can't even copy it but it shows up in the console window

<terminated> MainActivity (1) [Java Application] /System/Library/Frameworks/JavaVM.framework/Version/1.4/Home/bin/java (Nov 3, 2012 9:47:42 PM)

The bad part is that even if I erase the code and Clean the project that same error keeps occurring so code was that was working and I try this change see it stop working and remove the code sremains broken

The questions is two parts how do I fix this and what is the exact syntax I need to put in place so I can have my actions inside a method

Here is my original code which should work but it has no the for loop mentioned above is commented out this was actually working just not as I wanted it this my change but now it won't even run due to my last attempt at a fix

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends Activity {

ListView list;
LazyAdapter adapter;

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

    list=(ListView)findViewById(R.id.list);
    adapter=new LazyAdapter(this, mStrings);
    list.setAdapter(adapter);

    Button b=(Button)findViewById(R.id.button1);
    b.setOnClickListener(listener);
}

@Override
public void onDestroy()
{
    list.setAdapter(null);
    super.onDestroy();
}

public OnClickListener listener=new OnClickListener(){

    public void onClick(View arg0) {
        adapter.imageLoader.clearCache();
        adapter.notifyDataSetChanged();
    }
};    

String[] title = {
        "Abundance",
        "Anxiety",
        "Breakups",
        "Bruxism",
        "Decisions",
        "Zen"

};

 //I put my fix for the method issue here and eclipse blew up
 //I also uncommented the appropriate lines and commented out the mStrings declaration and
 //assignment



  //   String[] mStrings= new String[title.length];

 //   String urlbase = "http://somewhere.com/data/";
 //   String imgSel = "/logo.png";
 //   for(int i=0;i<title.length;i++){
 //     mStrings[i] =  urlbase + title[9].replaceAll("[^a-zA-Z]", "").toLowerCase() + imgSel;
 //   }

    //String[] mStrings = new String[title.length];

    private String[] mStrings={
        "http://somewhere.com/data/abundance/logo.png",
        "http://somewhere.com/data/anxiety/logo.png",
        "http://somewhere.com/data/breakups/logo.png",
        "http://somewhere.com/data/bruxism/logo.png",
        "http://somewhere.com/data/decisions/logo.png",
        "http://somewhere.com/data/zen/logo.png"
    };   
}
4

1 回答 1

1
public static void main(String[] args)

以上属于基本Java程序,不适用于Android。它处理自己的活动和其他程序入口点。

几乎所有代码都需要在方法中,尤其是if, while,for等语句。我们可以把这段代码放在 中onCreate,因为它会先被执行。

这是基本思想:

public class MainActivity extends Activity {
    // List / Adapter variables here

    private String[] title = {
        "Abundance",
        "Anxiety",
        "Breakups",
        "Bruxism",
        "Decisions",
        "Zen"
    };

    private String[] mStrings = new String[title.length];

    // OnClickListener here

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // super.onCreate and setContentView

        // Here is your for loop, untouched!
        String urlbase = "http://somewhere.com/data/";
        String imgSel = "/logo.png";
        for(int i=0;i<title.length;i++){
            mStrings[i] =  urlbase + title[9].replaceAll("[^a-zA-Z]", "").toLowerCase() + imgSel;
        }

        // More onCreate
    }

    // onDestroy method here
}
于 2012-11-04T06:43:30.773 回答