【发布时间】:2015-12-01 23:03:57
【问题描述】:
这段代码工作正常,但是当我将它变成一个类以通过稍后实例化它来使用它时,我得到了下面的错误。我知道这是因为我缺乏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 中是新手。
提前谢谢你
问候
【问题讨论】:
-
try { 这都是红色下划线。
-
嗯..这是因为您将代码放在类定义本身中...它需要在方法中...
-
谢谢哥们,我会检查的......
-
这很好用。解决了,谢谢...并致以最诚挚的问候