Getting Errors In My Activity Switching App

心已入冬 提交于 2019-12-11 16:31:09

问题


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!

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.


回答1:


As the error says, each Activity should be defined in its own .java file and declared in the AndroidManifest.xml.




回答2:


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.

Tip 1: Use Eclipse's auto formatting (Ctrl+shift+f), it will align opening & closing brackets with each other.

Tip 2: Highlight one bracket and Eclipse will highlight it's corresponding opening / closing bracket so you can see which belongs to which.

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.



来源:https://stackoverflow.com/questions/21357402/getting-errors-in-my-activity-switching-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!