【问题标题】:methods and actions inside class definition类定义中的方法和动作
【发布时间】:2012-11-04 06:13:26
【问题描述】:

需要注意的是,我对 android 编程相当陌生 我在使用这些行的字符串数组声明时遇到了一些问题

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

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

当我进行此更改时出现问题

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

然后在 System.out 行我看到了这个错误

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

这让我和其他人困惑了很长时间,直到我发现我没有方法的东西,前面的所有陈述都是在类声明中进行的。

***在这里学到了一些东西

我在另一篇文章中看到了这个并尝试了他们的解决方案,即添加这个

public static void main(String[] args) {


}

围绕我的“行动”,这看起来是正确的并且有道理,所以我尝试了它。 当我这样做时,日食吹了一个螺丝,没有给我一些非常奇怪的东西 我什至无法复制它,但它显示在控制台窗口中

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

不好的部分是,即使我删除代码并清理项目,同样的错误仍然不断发生,所以代码是有效的,我尝试这个更改,发现它停止工作并删除代码仍然损坏

问题分为两部分,我该如何解决这个问题,以及我需要使用的确切语法是什么,以便我可以在方法中执行操作

这是我的原始代码,它应该可以工作,但它没有上面提到的 for 循环被注释掉了这实际上是在工作,只是不像我想要的那样,这是我的更改,但现在由于我最后一次尝试,它甚至不会运行修复

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

【问题讨论】:

  • 您的帖子存在多个问题。它太长了,漫无边际,既不包含具体问题,也不包含对问题的具体描述。您还使用了很多不正确的术语。据我所知,您需要一些 Java 教程。如果正确使用分号和大括号给您带来了问题,那么基本的 Java 使用,而不是 Android 编程,就是您的问题。投票结束。
  • 好吧,西蒙无论如何都成功回答了。它不是分号或括号 这只是所指出的。问题是语句被意外放置在任何方法之外,并且只是存在于类声明中,这里的其他几个人也错过了一些东西。但我非常感谢您查看
  • 奇怪的是,添加某行代码可能会在 eclipse 中破坏它,以至于即使删除该行代码也可以解决 eclipse 问题

标签: android arrays class methods


【解决方案1】:
public static void main(String[] args)

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

几乎所有代码都需要在方法中,尤其是ifwhilefor 等语句。我们可以将这段代码放在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
}

【讨论】:

  • 非常感谢我花了最后三天与这件事有关的事情我终于能够取得进展
猜你喜欢
  • 1970-01-01
  • 2015-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-10
  • 2020-11-03
  • 2022-12-23
相关资源
最近更新 更多