简体   繁体   中英

The code that works gives errors when I try to carry it into a class

This code works fine but when I turn it into a class to use it by instantiating it later I get the errors below. I know this is because I lack the basics of android development.:

      import android.content.res.AssetManager;
      import android.os.Bundle;
      import android.support.v7.app.AppCompatActivity;
      import android.widget.TextView;
      import android.widget.Toast;

      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStream;
      import java.io.InputStreamReader;

      public class MainActivity extends AppCompatActivity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);



           AssetManager manager;
           String line = null;
           String[] bilgi = new String[20];
           String[] ara = new String[20];
           String[] hamle = new String[1000];



           InputStream is = null;
           InputStreamReader isr = null;
           BufferedReader br = null;
           int don = -1;
           int don2 = -1;
           int kontrol = 0;

           try {
               manager = getAssets();
               is = manager.open("game366178.pgn");
               isr = new InputStreamReader(is);
               br = new BufferedReader(isr);
               while ((line = br.readLine()) != null) {

                   don++;
                   if (kontrol == 0) {
                       bilgi[don] = line;
                   } else {
                       ara = (line.split(" "));
                       for (int z = 0; z < ara.length; z++) {
                           don2++;
                           hamle[don2] = ara[z];
                       }
                   }

                    if (line.isEmpty()) {
                       kontrol = don;
                   }


               }


               TextView mytextview = (TextView) findViewById(R.id.showarray);
               mytextview.setText(bilgi[8] + "  kontrol equals to:  " +     kontrol);

            } catch (IOException e1) {
               Toast.makeText(getBaseContext(), "Problem!",    Toast.LENGTH_LONG).show();
            } finally {

                try {
                //if(reader != null)
                //  reader.close();
                   if (br != null)
                       br.close();
                    if (isr != null)
                       isr.close();
                    if (is != null)
                       is.close();
                 } catch (IOException ex) {
                // dosomething
                    ex.printStackTrace();
                }


           }


        }
    }

But when I try to make a class out of it, we get an error =>

package chessactivesekizsatirvetekliarray.com.sekizsatirvetekliarray;

   import android.content.res.AssetManager;
   import android.os.Bundle;
   import android.support.v7.app.AppCompatActivity;
   import android.widget.TextView;
   import android.widget.Toast;

   import java.io.BufferedReader;
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.InputStreamReader;

/**
 * Created by serhat on 01.12.2015.
 */
   public class hamleArrayi extends AppCompatActivity   {


       AssetManager manager;
       String line = null;
       String[] bilgi = new String[20];
       String[] ara = new String[20];
       String[] hamle = new String[1000];



       InputStream is = null;
       InputStreamReader isr = null;
       BufferedReader br = null;
       int don = -1;
       int don2 = -1;
       int kontrol = 0;

       try {
           manager = getAssets();
           is = manager.open("game366178.pgn");
           isr = new InputStreamReader(is);
           br = new BufferedReader(isr);
           while ((line = br.readLine()) != null) {

               don++;
               if (kontrol == 0) {
                   bilgi[don] = line;
               } else {
                   ara = (line.split(" "));
                   for (int z = 0; z < ara.length; z++) {
                       don2++;
                       hamle[don2] = ara[z];
                   }
               }

               if (line.isEmpty()) {
                   kontrol = don;
              }


           }


           // TextView mytextview = (TextView) findViewById(R.id.showarray);
           //mytextview.setText(bilgi[8] + "  kontrol equals to:  " + kontrol);

       }

       catch (IOException e1)

       {
           Toast.makeText(getBaseContext(), "Problem!", Toast.LENGTH_LONG).show();
      }

       finally

       {

           try {
               //if(reader != null)
               //  reader.close();
               if (br != null)
                   br.close();
               if (isr != null)
                  isr.close();
               if (is != null)
                      is.close();
               } catch (IOException ex) {

                 ex.printStackTrace();
               }


           }

       }

When I clean the project to see what is wrong I get =>

  D:\Android_Dosyalar\Proje\SekizSatirVeTekliArray\app\src\main\java  
    \chessactivesekizsatirvetekliarray\com\sekizsatirvetekliarray
     \hamleArrayi.java
       Error:(37, 5) error: illegal start of type
       Error:(37, 8) error: ';' expected
       Error:(38, 16) error: <identifier> expected
       Error:(39, 11) error: <identifier> expected
       Error:(40, 12) error: <identifier> expected
       Error:(41, 11) error: <identifier> expected
       Error:(42, 9) error: illegal start of type
       Error:(42, 16) error: illegal start of type
       Error:(42, 17) error: ')' expected
       Error:(42, 21) error: ';' expected
       Error:(42, 35) error: <identifier> expected
       Error:(42, 37) error: ';' expected
       Error:(68, 5) error: class, interface, or enum expected
       Error:(72, 5) error: class, interface, or enum expected
       Error:(83, 13) error: class, interface, or enum expected
       Error:(85, 13) error: class, interface, or enum expected
       Error:(87, 9) error: class, interface, or enum expected
       Error:(90, 9) error: class, interface, or enum expected
       Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
       > Compilation failed; see the compiler error output for details.
       Information:BUILD FAILED

I copied and pasted the code. I have intermediate java but novice in android. Thank you in advance
Regards

The class you make doesn't have a method.

 public class hamleArrayi extends AppCompatActivity   {


   AssetManager manager;.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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