将变量从 activity 传递到非 activity class

Passing a variable from an activity to a non-activity class

在我的 Main Activity 中,我有这部分包含所选文件的文件路径和文件名。

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

我认为这行会抛出 NullPointException

File file = MainActivity.Filename;

如果您想将文件路径从 MainActivity.java 传递到 CoordinatesXmlParser.java,您可以使用应用程序 Class 来实现。

在你的应用程序Class中,你可以写两个方法

 private String fileName;


public String getFileName() {
    return fileName;
}


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

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

它 returns 为空,因为您在任何时候都为变量 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。