将变量从活动传递到非活动类

时间:2015-03-30 02:10:26

标签: android android-activity filenames filepath

在我的主要活动中,我有这个部分,其中包含文件路径和所选文件的文件名。

public class MainActivity extends Activity implements OnClickListener{

private static File selectedFile;

public static File Filename;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(resultCode == RESULT_OK) {

        switch(requestCode) {

        case REQUEST_PICK_FILE:

            if(data.hasExtra(FilePicker.EXTRA_FILE_PATH)) {

                selectedFile = new File
                        (data.getStringExtra(FilePicker.EXTRA_FILE_PATH));
                filePath.setText(selectedFile.getPath());  
            }
            break;
        }
    }
} //onActivityResult

//and i added this so that I will be able to save the filepath and access it from my CoordinatesXmlParser.java (a non-activity class)

public void Filename () {
String filename = selectedFile.toString();

//return selectedFile;
}
}

然后在我的CoordinatesXmlParser.java上,我有这个部分:

File file = MainActivity.Filename;
file.toString();

//String file = Environment.getExternalStorageDirectory() + "/XML/coordinates.xml";
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
FileInputStream fis = new FileInputStream(file);
parser.setInput(new InputStreamReader(fis));

问题:它正在抛出一个NullPointException。我怎样才能将文件路径从MainActivity.java传递给CoordinatesXmlParser.java? TIA

2 个答案:

答案 0 :(得分:1)

我认为这一行会抛出一个NullPointException

  

文件file = MainActivity.Filename;

如果要将文件路径从MainActivity.java传递给CoordinatesXmlParser.java,可以使用Application Class来实现此目的。

在Application Class中,您可以编写两个方法

 private String fileName;


public String getFileName() {
    return fileName;
}


public void setFileName(String fileName) {
    this.fileName = fileName;
}

然后在你的MainActivity中调用setFileName方法,并在你的CoordinatesXmlParser中调用get getFileName方法。

答案 1 :(得分:0)

它返回Null,因为在任何时刻你都为变量Filename分配了任何值(顺便说一下,它违背了模式:变量不应该以大写字母开头)。

public class MainActivity extends Activity implements OnClickListener{

private static File selectedFile;

public static File Filename;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(resultCode == RESULT_OK) {

        switch(requestCode) {

        case REQUEST_PICK_FILE:

            if(data.hasExtra(FilePicker.EXTRA_FILE_PATH)) {

                selectedFile = new File
                        (data.getStringExtra(FilePicker.EXTRA_FILE_PATH));
                filePath.setText(selectedFile.getPath());  
            }
            break;
        }
    }
} //onActivityResult

//and i added this so that I will be able to save the filepath and access it from my CoordinatesXmlParser.java (a non-activity class)
     public static String getFilename () {
         return selectedFile.toString();
     }
}

然后

File file = MainActivity.getFilename();
file.toString();

此外,重要的是要注意这根本不是一个好主意。只有在确定已经调用了“onActivityResult”之后才应该调用getFilename(),否则它将始终引发NullPointerException。