您好,我试图解决我的应用程序,具有一个进度条,以及一个按钮,切换到新的活动。可能有人请帮我找出我的错误,给我一个解决问题的办法?我很新的机器人编程!
Hello I am trying to fix my app that features a progress bar as well as a button that switches to a new activity. Could someone please help me isolate my errors and give me a solution to the problem? I am very new at android programming!
下面是我的主要活动。我已经加粗的地方我收到错误。在这里,我得到一个错误说活动1必须在它自己的文件中定义的
Here is my main activity. I have bolded where I am receiving errors. Here I get an error saying "Activity 1 Must be defined in its own file"
package com.example.progressdialog;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
public class mainactivity2 {
}
private ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progress = new ProgressDialog(this);
}
public void open(View view){
progress.setMessage("Progressing Along!");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(false);
progress.show();
final int totalProgressTime = 100;
final Thread t = new Thread(){
@Override
public void run(){
int jumpTime = 0;
while(jumpTime < totalProgressTime){
try {
sleep(500);
jumpTime += 1;
progress.setProgress(jumpTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
t.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
public class **Activity1** extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button next = (Button) findViewById(R.id.button2);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
下面是我的第二个活动的文件。我再次加粗我在哪里有错误。这人说我缺少一个支架来完成方法体,但我似乎无法搞清楚在哪里把它。
Here is my second activity file. I once again bolded where I am having errors. This one says I am missing a bracket to complete method body, but I cannot seem to figure out where to put it.
package com.example.progressdialog;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Activity2 extends Activity **{**
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button next = (Button) findViewById(R.id.button1);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
})**;**
感谢您!我只需要弄清楚如何纠正这些错误,因为我以前接到过几次,如果你能解释为什么错误发生,这将有助于我也。
Thank you! I just need to figure out how to correct these errors because I have gotten them a few times before, if you could explain why the errors are occurring that would help me also.
只要把右括号在你活动结束。你似乎缺少BTW不是一个而是两个右括号。打开支架每次 {
你还必须关闭它}
。
括号用于构建code,例如定义一个方法体或类。
Just put the closing brackets at the end of your activity. You seem to be missing not one but two closing brackets btw. Every time you open a bracket {
you also have to close it }
.
Brackets are used to structure code, for example to define a method body or a class.
提示1:使用Eclipse的自动格式化(控制 + 移动 + F ),这将调整开放和放大器;闭括号彼此。
Tip 1: Use Eclipse's auto formatting (Ctrl+shift+f), it will align opening & closing brackets with each other.
提示2:突出一个支架和Eclipse将突出它对应的打开/关闭支架,使您可以看到哪些属于哪个。
Tip 2: Highlight one bracket and Eclipse will highlight it's corresponding opening / closing bracket so you can see which belongs to which.
您会得到如何code由支架结构很快就感觉;)
You'll get a feeling for how code is structured by brackets soon ;)
此外,如灵光回答,每个活动应该在它自己的文件中声明。
Also, as Emmanuel answered, every Activity should be declared in it's own file.