我正在使用android studio和名为myCBR的外部库。我正在尝试加载放在我的资源文件夹中的文件。我有一个名为“MainActivity”和“CBRHandler”的主要活动。我可以运行应用程序但是当我尝试通过切换按钮加载项目时,我的应用程序崩溃了。你知道可能导致它的原因吗?
public class MainActivity extends Activity {
Boolean InternetAvailable = false;
Button checkInternet;
DetectConnection detect_connection;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10L;
private static final long MIN_TIME_PASSED_FOR_UPDATES = 10000L;
private static final String PRJ_FILENAME = "financeDemo.prj";
private static final String PROJECTNAME = "financeDemo";
final String TAG = "[HUB]";
protected CBRHandler cbrHandler = null;
private AsyncTask<String, Integer, String> cbrTask;
protected HashMap<String, String> currentLocales;
protected Location currentLocation;
protected ProgressDialog dialog;
protected LocationManager locManager;
protected Button locationButton;
protected String locationlatitude;
protected String locationlongitude;
protected TextView myCBRtext;
protected ToggleButton myCBRtoggleButton;
protected Button networkButton;
protected TextView networkText;
protected Button socialProfileButton;
protected TextView socialText;
protected Button startQueryButton;
protected Button userDetailsButton;
@Override
protected void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout.activity_main);
//Setting up button for "My Librarian" section
Button MyLibrarian = (Button) findViewById(R.id.btnMyLibrarian);
MyLibrarian.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MyLibrarianActivity.class));
}
});
//Setting up button for "Around Me" section
Button AroundMe = (Button) findViewById(R.id.btnAroundMe);
AroundMe.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, AroundMeActivity.class));
}
});
//Setting up button for "Find your way" section
Button FindYourWay = (Button) findViewById(R.id.btnFindYourWay);
FindYourWay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, FindYourWayActivity.class));
}
});
//Setting up button for "Author" section
Button Author = (Button) findViewById(R.id.btnAuthor);
Author.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, AuthorActivity.class));
}
});
//Setting up "Check for network connectivity button"
checkInternet = (Button) findViewById(R.id.btnNetworkConnectivity);
detect_connection = new DetectConnection(getApplicationContext());
checkInternet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InternetAvailable = detect_connection.InternetConnecting();
if (InternetAvailable) {
showAlertDialog(MainActivity.this, "Network Connection",
"Network connection is available", true);
} else {
showAlertDialog(MainActivity.this, "No Network Connection",
"Network connection is not available", false);
}
}
});
{
this.myCBRtoggleButton = ((ToggleButton) findViewById(R.id.myCBRtoggleButton));
this.myCBRtoggleButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View paramAnonymousView) {
if (MainActivity.this.myCBRtoggleButton.isChecked()) {
Toast.makeText(MainActivity.this, "Starting a myCBR task", Toast.LENGTH_SHORT).show();
MainActivity.this.cbrTask = new MainActivity.OpenMyCBRProjectTask(MainActivity.this).execute(new String[0]);
return;
}
Toast.makeText(MainActivity.this, "Resetting", Toast.LENGTH_SHORT).show();
MainActivity.this.cbrTask.cancel(true);
MainActivity.this.startQueryButton.setEnabled(false);
CBRHandler.resetInstance();
}
});
this.startQueryButton = ((Button) findViewById(R.id.startQueryButton));
this.startQueryButton.setEnabled(false);
this.startQueryButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View paramAnonymousView) {
Intent localIntent = new Intent(MainActivity.this, MyLibrarianActivity.class);
MainActivity.this.startActivity(localIntent);
}
});
FileIO.copyAssetFile(this, getResources(), "libraryDemo.prj");
return;
}
}
class OpenMyCBRProjectTask extends AsyncTask<String, Integer, String> {
OpenMyCBRProjectTask() {
}
public OpenMyCBRProjectTask(MainActivity mainActivity) {
}
protected String doInBackground(String[] paramArrayOfString) {
MainActivity.this.cbrHandler = CBRHandler.getInstance();
try {
MainActivity.this.cbrHandler.createProjectFromPRJ("libraryDemo", "libraryDemo.prj", "data/data/" + MainActivity.this.getPackageName() + "/files/");
return "Opening project 'libraryDemo'";
} catch (Exception localException) {
Log.e("[HUB]", "Exception caught while trying to call createProjectFromPRJ");
Log.e("[HUB]", localException.toString());
}
return "Failed to open project";
}
protected void onPostExecute(String paramString) {
MainActivity.this.myCBRtext.setText("Project: 'libraryDemo'");
MainActivity.this.startQueryButton.setEnabled(true);
}
protected void onProgressUpdate(Integer[] paramArrayOfInteger) {
super.onProgressUpdate(paramArrayOfInteger);
}
}
public void showAlertDialog(Context context, String t, String m, Boolean status) {
AlertDialog message = new AlertDialog.Builder(context).create();
message.setTitle(t);
message.setMessage(m);
message.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
message.show();
}
}
我的CBRHandler活动
public class CBRHandler
{
private static CBRHandler singletonInstance;
private final String TAG = "[CBR]";
private Concept concept = null;
private String conceptName = "Resource";
private DefaultCaseBase ResourcesCB;
private String multiplevalueseparator = ",";
private String prjFileName = "";
private Project project = null;
private LinkedHashMap<String, String> queryAttributes = new LinkedHashMap();
public static CBRHandler getInstance()
{
if (singletonInstance == null)
singletonInstance = new CBRHandler();
return singletonInstance;
}
public static void resetInstance()
{
singletonInstance = null;
}
public void addQueryAttribute(String paramString1, String paramString2)
{
if (this.queryAttributes.containsKey(paramString1))
this.queryAttributes.remove(paramString1);
this.queryAttributes.put(paramString1, paramString2);
}
public void createProjectFromPRJ(String paramString1, String paramString2, String paramString3)
{
try
{
Log.i("[CBR]", "Assigning project: " + paramString3 + paramString2);
this.project = new Project(paramString3 + paramString2);
Log.i("[CBR]", "Assigning concept: " + this.conceptName);
this.concept = this.project.getConceptByID(this.conceptName);
return;
}
catch (Exception localException)
{
Log.e("[CBR]", "Failed to create project from .prj");
Log.e("[CBR]", localException.toString());
}
}
public boolean hasProject()
{
return this.project != null;
}
}
我得到的例外是:
1162-1162/dyankov.mylibraryrecommender E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at dyankov.mylibraryrecommender.MainActivities.MainActivity$OpenMyCBRProjectTask.onPostExecute(MainActivity.java:245)
at dyankov.mylibraryrecommender.MainActivities.MainActivity$OpenMyCBRProjectTask.onPostExecute(MainActivity.java:217)
答案 0 :(得分:2)
在AsyncTask的onPostExecute
方法中,您可以执行以下操作:
protected void onPostExecute(String paramString) {
MainActivity.this.myCBRtext.setText("Project: 'libraryDemo'");
MainActivity.this.startQueryButton.setEnabled(true);
}
您永远不会正确初始化myCBRtext
。您可以在onCreate
中执行此操作:
@Override
protected void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout.activity_main);
// Add the following
this.myCBRtext = (TextView) findViewById(R.id.my_cbr_text);
....
}