我在Android应用程序中创建项目。我已经在java eclipse环境中将java jdt ast作为独立应用程序实现了,我得到了我在控制台输出上所需要的东西。现在我想将该代码运行到我的android应用程序项目中。但是当我启动应用程序时在android的textView中设置所有输出,不幸的是它停止了,我不知道为什么...... 有人会知道为什么会这样吗? 谢谢你的帮助。
***
代码没有显示错误,但应用程序无法运行...
现在我想到另一个问题:我们真的可以使用java jdt ast作为Android应用程序吗?导入相同的库,但是当它用作Java应用程序项目时,代码可以正常运行。
在任何情况下,这里都是来自logCat的堆栈跟踪。
package com.example.uiui;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.Expression;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
static TextView tv1;
private String filename = "MySampleFile.java";
private static String filepath = "/MyFileStorage";
File myExternalFile = new File(Environment.getExternalStorageDirectory(),filepath );
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView)findViewById(R.id.txt1);
tv1.setMovementMethod(new ScrollingMovementMethod());
Button readFromExternalStorage =
(Button) findViewById(R.id.getExternalStorage);
readFromExternalStorage.setOnClickListener(this);
//check if external storage is available and not read only
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
}
else {
myExternalFile = new File(getExternalFilesDir(filepath), filename);
}
}
@Override
public void onClick(View v) {
// EditText myInputText = (EditText) findViewById(R.id.myInputText);
TextView responseText = (TextView) findViewById(R.id.responseText);
String myData = "";
switch (v.getId()) {
case R.id.getExternalStorage:
try {
FileInputStream fis = new FileInputStream(myExternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine;
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
// myInputText.setText(myData);
responseText
.setText("MySampleFile.txt data retrieved from Internal Storage...");
parse(myData);
break;
}
}
public void parse(String str) {
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setSource(str.toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setResolveBindings(true);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
Set names = new HashSet();
public boolean visit(VariableDeclarationFragment node) {
SimpleName name = node.getName();
this.names.add(name.getIdentifier());
tv1.setText(tv1.getText()+"Declaration of '" + name + "' at line"+cu.getLineNumber(name.getStartPosition())+"\n");
return false; // do not continue
}
public boolean visit(SimpleName node) {
if (this.names.contains(node.getIdentifier())) {
tv1.setText(tv1.getText()+"Usage of '" + node + "' at line "+ cu.getLineNumber(node.getStartPosition())+"\n");
}
return true;
}
public boolean visit(MethodInvocation node) {
System.out.println("Method Name: " + node.getName()+"\n");
Expression expression = node.getExpression();
if (expression != null) {
tv1.setText(tv1.getText()+"Expression: " + expression.toString()+"\n");
ITypeBinding typeBinding = expression.resolveTypeBinding();
if (typeBinding != null) {
tv1.setText(tv1.getText()+"Type: " + typeBinding.getName());
}
}
IMethodBinding binding = node.resolveMethodBinding();
if (binding != null) {
ITypeBinding type = binding.getDeclaringClass();
if (type != null) {
tv1.setText(tv1.getText()+"Decl: " + type.getName());
}
}
return true;
}
});
}
private static boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}
private static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
}
这是我的资源布局文件:
09-01 03:38:05.780: E/AndroidRuntime(21566): FATAL EXCEPTION: main
09-01 03:38:05.780: E/AndroidRuntime(21566): java.lang.NoClassDefFoundError: org.eclipse.jdt.core.dom.ASTParser
09-01 03:38:05.780: E/AndroidRuntime(21566): at com.example.uiui.MainActivity.parse(MainActivity.java:98)
09-01 03:38:05.780: E/AndroidRuntime(21566): at com.example.uiui.MainActivity.onClick(MainActivity.java:86)
09-01 03:38:05.780: E/AndroidRuntime(21566): at android.view.View.performClick(View.java:4475)
09-01 03:38:05.780: E/AndroidRuntime(21566): at android.view.View$PerformClick.run(View.java:18786)
09-01 03:38:05.780: E/AndroidRuntime(21566): at android.os.Handler.handleCallback(Handler.java:730)
09-01 03:38:05.780: E/AndroidRuntime(21566): at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 03:38:05.780: E/AndroidRuntime(21566): at android.os.Looper.loop(Looper.java:176)
09-01 03:38:05.780: E/AndroidRuntime(21566): at android.app.ActivityThread.main(ActivityThread.java:5419)
09-01 03:38:05.780: E/AndroidRuntime(21566): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 03:38:05.780: E/AndroidRuntime(21566): at java.lang.reflect.Method.invoke(Method.java:525)
09-01 03:38:05.780: E/AndroidRuntime(21566): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
09-01 03:38:05.780: E/AndroidRuntime(21566): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
09-01 03:38:05.780: E/AndroidRuntime(21566): at dalvik.system.NativeStart.main(Native Method)
最后是manifest.xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.uiui.MainActivity" >
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:layout_marginTop="10dp"
android:maxLines="200"
android:scrollbars="vertical"
android:text="TextView" />
<Button
android:id="@+id/getExternalStorage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txt1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="18dp"
android:text="Display from External Storage" />
<EditText
android:id="@+id/myInputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/getExternalStorage"
android:layout_alignLeft="@+id/getExternalStorage"
android:layout_marginBottom="75dp"
android:ems="10"
android:gravity="top|left"
android:inputType="textMultiLine"
android:lines="5"
android:minLines="3" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/responseText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txt1"
android:layout_below="@+id/txt1"
android:layout_marginTop="18dp"
android:maxLines="200"
android:scrollbars="vertical"
android:text=" ad"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
答案 0 :(得分:2)
您没有在ParseFilesInDir()
中指出崩溃的位置。但是,这条线在Android中是完全错误的:
File dirs = new File(".");
首先,您需要决定将文件复制到Android设备的位置。您的Android设备不是您的开发PC,因此该Android设备上没有Java源代码,除非您将其复制到那里。
然后,无论您将文件放在何处,都需要调整ParseFilesInDir()
以匹配。
放置文件位置的两个主要选项是internal storage和external storage。如果要复制到外部存储器,通常可以更轻松地将文件复制到设备上。