This is the main activity I am trying to move like classes to a new file Once I do, I am not able to successfully call the class The following code generates a null pointer, though the pointer is not null I cannot simply change the savedTrainInfo class to static because then the file routines will not work. There must be a simple solution I need to keep my classes separate for clarity.
import mp.trains.conductor.savedTrainInfo;
public class trains extends Activity
{
private savedTrainInfo msavedTrainInfo;
protected static Context context;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
trains.context=getApplicationContext();
msavedTrainInfo=new savedTrainInfo();
msavedTrainInfo.getTrainAttributes();
}
}
new file many functions here; mainly file I/O These all work fine if there are located in the main activity trains You are correct. The error seems to be in if (readStartupFile(trainAttributes)==false)
public class savedTrainInfo extends trains
{
public void getTrainAttributes()
{
trainAttributes=new String [10];
if (readStartupFile(trainAttributes)==false)
{
fileLine=0;
}
else
{
try
{
fileLine = Integer.parseInt(trainAttributes[0].toString());
}
catch(NumberFormatException nfe)
{
System.out.println("Could not parse " + nfe);
}
}
}
public boolean readStartupFile(String objects[])
{
try
{
// InputStream fHandle = openFileInput("startupTrains.txt");
InputStream fHandle = trains.context.openFileInput("startupTrains.txt");
if (fHandle != null)
{
// prepare the file for reading
InputStreamReader inputreader = new InputStreamReader(fHandle);
BufferedReader buffreader = new BufferedReader(inputreader);
String line;
line = buffreader.readLine();
if (line == null)
{
fHandle.close();
return false;
}
else
{
String[] item = (line).split(",");
obj开发者_如何学Pythonects[0]=item[0].toString();
fHandle.close();
}
return true;
}
else
{
return false;
}
}
catch (FileNotFoundException e2)
{
return false;
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
}
}
精彩评论