这段代码工作正常,但是当我把它变成一个类来通过实例化它来使用它时,我得到下面的错误。我知道这是因为我缺乏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();
}
}
}
当我清理项目以查看错误时,我得到=&gt;
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
我复制并粘贴了代码。我有android的中级java但是新手。 提前谢谢你 此致
答案 0 :(得分:1)
你制作的课程没有方法。
public class hamleArrayi extends AppCompatActivity {
AssetManager manager;.....