java - 当我尝试将有效的代码带入类中时,它会出现错误

标签 java android class object

这段代码工作正常,但是当我将它变成一个类并通过稍后实例化它来使用它时,我收到以下错误。我知道这是因为我缺乏android开发的基础。:

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


           }

       }

当我清理项目以查看问题所在时,我得到 =>

  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

我复制并粘贴了代码。我有中级java但android新手。 预先感谢您
问候

最佳答案

您创建的类没有方法。

 public class hamleArrayi extends AppCompatActivity   {


   AssetManager manager;.....

关于java - 当我尝试将有效的代码带入类中时,它会出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34031896/

相关文章:

java - Android Studio (Intellij) 编译错误

ios - 如何让大类更优雅?

python - 无法访问类属性

java - 在java中序列化的基本组件是什么?

java - 如何从属性文件读取列表值

android - 在 WebView android 中检测空白网页

android - android中的camera和camera2权限

javascript - javascript 类的原型(prototype)语法错误

java - BigDecimal 构造函数性能 - 字符串与数字

java - 如果我将扩展 Activity 更改为 OrmLiteBaseActivity<Helper> 我得到一个 java.lang.NoClassDefFoundError