我正在尝试从 sdcard“VCFFiles”中的特定文件夹发送文件。我做的一切都是正确的。当我运行这个应用程序时,它再次工作,它显示强制关闭。我在项目中所做的是我制作了 5 个单选按钮并将它们作为文件列在“VCFFiles”文件夹中的文本。当单击任何单选按钮时,相应的 vcf 文件将被发送到单击发送按钮时在编辑文本中输入的电子邮件中。以下是我的代码:
public class SendActivity extends Activity{
private static final int PICK_IMAGE = 1;
EditText email;
TextView add_manage;
Button send;
RadioGroup vcfgroup;
RadioButton vcfButton;
String filepath="";
static File filevcf;
static Context mContext;
RadioButton vcf1;
RadioButton vcf2;
RadioButton vcf3;
RadioButton vcf4;
RadioButton vcf5;
ProgressDialog dialog;
String success;
String message;
String response;
String data;
File f;
String radio;
ArrayList<String> array;
// String state = Environment.getExternalStorageState();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.send);
email = (EditText)findViewById(R.id.emailid);
send = (Button)findViewById(R.id.send);
add_manage = (TextView)findViewById(R.id.add_manage);
vcfgroup = (RadioGroup)findViewById(R.id.vcfgroup);
vcf1 = (RadioButton)findViewById(R.id.vcf1);
vcf2 = (RadioButton)findViewById(R.id.vcf2);
vcf3 = (RadioButton)findViewById(R.id.vcf3);
vcf4 = (RadioButton)findViewById(R.id.vcf4);
vcf5 = (RadioButton)findViewById(R.id.vcf5);
send = (Button)findViewById(R.id.send);
// get selected radio button from radioGroup
radio = vcf1.getText().toString();
//get file names and set it in radio buttons
if(!isExternalStorageAvailable() || isExternalStorageReadOnly()){
}else{
new EventListTask2().execute(this);
}
//send vcf files to the server
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int selectedId = vcfgroup.getCheckedRadioButtonId();
vcf1 =(RadioButton)findViewById(selectedId);
try{
f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/VCFFiles/"+vcf1.getText().toString());
filepath = f.getPath();
if(email.getText().length()!=0){
dialog = ProgressDialog.show(SendActivity.this,
"Sending", "Please wait...", true);
new EventListTask1().execute();
}else{
Toast.makeText(getApplicationContext(), "You may have not entered emailid or selected wrong file!!", Toast.LENGTH_LONG).show();
}
}catch(Exception e){
}
}
});
add_manage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in = new Intent(SendActivity.this,AddContactActivity.class);
startActivity(in);
}
});
}
public ArrayList<String> GetFiles() {
ArrayList<String> MyFiles = new ArrayList<String>();
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/VCFFiles");
f.mkdirs();
File[] files = f.listFiles();
if (files.length == 0)
return null;
else {
for (int i=0; i<5; i++)
if(files[i].getPath().endsWith(".vcf")){
MyFiles.add(files[i].getName());
}
}
return MyFiles;
}
在 EventListTask2 类中,我使用 GetFiles() 函数从“VCFFIles”中获取文件,并将名称与每个单选按钮的名称相关联。里面的代码如下所示:
RadioButton radioset[] = {vcf1, vcf2, vcf3, vcf4, vcf5};
if(GetFiles().size()!=0){
array=GetFiles();
for(int i=0;i<array.size();i++){
radioset[i].setText(array.get(i));
}}else{
Toast.makeText(getApplicationContext(), "You have no vcf files saved", Toast.LENGTH_LONG).show();
}
以下是应用程序的日志:
03-18 18:41:17.410: E/AndroidRuntime(1598): FATAL EXCEPTION: main
03-18 18:41:17.410: E/AndroidRuntime(1598): java.lang.NullPointerException
03-18 18:41:17.410: E/AndroidRuntime(1598): at com.example.mycontactdetails.SendActivity$EventListTask1.onPostExecute(SendActivity.java:321)
03-18 18:41:17.410: E/AndroidRuntime(1598): at com.example.mycontactdetails.SendActivity$EventListTask1.onPostExecute(SendActivity.java:1)
03-18 18:41:17.410: E/AndroidRuntime(1598): at android.os.AsyncTask.finish(AsyncTask.java:417)
03-18 18:41:17.410: E/AndroidRuntime(1598): at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-18 18:41:17.410: E/AndroidRuntime(1598): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-18 18:41:17.410: E/AndroidRuntime(1598): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 18:41:17.410: E/AndroidRuntime(1598): at android.os.Looper.loop(Looper.java:123)
03-18 18:41:17.410: E/AndroidRuntime(1598): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-18 18:41:17.410: E/AndroidRuntime(1598): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 18:41:17.410: E/AndroidRuntime(1598): at java.lang.reflect.Method.invoke(Method.java:521)
03-18 18:41:17.410: E/AndroidRuntime(1598): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-18 18:41:17.410: E/AndroidRuntime(1598): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-18 18:41:17.410: E/AndroidRuntime(1598): at dalvik.system.NativeStart.main(Native Method)
03-18 18:53:12.560: E/IOException(1648): IO error: /mnt/sdcard/VCFFiles/Empty (Permission denied)
03-18 18:53:12.560: E/IOException(1648): java.io.FileNotFoundException: /mnt/sdcard/VCFFiles/Empty (Permission denied)
03-18 18:53:12.560: E/IOException(1648): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
03-18 18:53:12.560: E/IOException(1648): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
03-18 18:53:12.560: E/IOException(1648): at java.io.FileInputStream.<init>(FileInputStream.java:82)
03-18 18:53:12.560: E/IOException(1648): at com.example.mycontactdetails.SendActivity$Utils.PostDataToFavoriteUser(SendActivity.java:182)
03-18 18:53:12.560: E/IOException(1648): at com.example.mycontactdetails.SendActivity$EventListTask1.doInBackground(SendActivity.java:310)
03-18 18:53:12.560: E/IOException(1648): at com.example.mycontactdetails.SendActivity$EventListTask1.doInBackground(SendActivity.java:1)
03-18 18:53:12.560: E/IOException(1648): at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-18 18:53:12.560: E/IOException(1648): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-18 18:53:12.560: E/IOException(1648): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-18 18:53:12.560: E/IOException(1648): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-18 18:53:12.560: E/IOException(1648): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-18 18:53:12.560: E/IOException(1648): at java.lang.Thread.run(Thread.java:1096)
03-18 18:53:12.630: E/AndroidRuntime(1648): FATAL EXCEPTION: main
03-18 18:53:12.630: E/AndroidRuntime(1648): java.lang.NullPointerException
03-18 18:53:12.630: E/AndroidRuntime(1648): at com.example.mycontactdetails.SendActivity$EventListTask1.onPostExecute(SendActivity.java:321)
03-18 18:53:12.630: E/AndroidRuntime(1648): at com.example.mycontactdetails.SendActivity$EventListTask1.onPostExecute(SendActivity.java:1)
03-18 18:53:12.630: E/AndroidRuntime(1648): at android.os.AsyncTask.finish(AsyncTask.java:417)
03-18 18:53:12.630: E/AndroidRuntime(1648): at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-18 18:53:12.630: E/AndroidRuntime(1648): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-18 18:53:12.630: E/AndroidRuntime(1648): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 18:53:12.630: E/AndroidRuntime(1648): at android.os.Looper.loop(Looper.java:123)
03-18 18:53:12.630: E/AndroidRuntime(1648): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-18 18:53:12.630: E/AndroidRuntime(1648): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 18:53:12.630: E/AndroidRuntime(1648): at java.lang.reflect.Method.invoke(Method.java:521)
03-18 18:53:12.630: E/AndroidRuntime(1648): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-18 18:53:12.630: E/AndroidRuntime(1648): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-18 18:53:12.630: E/AndroidRuntime(1648): at dalvik.system.NativeStart.main(Native Method)
03-18 19:00:13.010: E/IOException(1676): IO error: /mnt/sdcard/VCFFiles/Empty (Permission denied)
03-18 19:00:13.010: E/IOException(1676): java.io.FileNotFoundException: /mnt/sdcard/VCFFiles/Empty (Permission denied)
03-18 19:00:13.010: E/IOException(1676): at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
03-18 19:00:13.010: E/IOException(1676): at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
03-18 19:00:13.010: E/IOException(1676): at java.io.FileInputStream.<init>(FileInputStream.java:82)
03-18 19:00:13.010: E/IOException(1676): at com.example.mycontactdetails.SendActivity$Utils.PostDataToFavoriteUser(SendActivity.java:183)
03-18 19:00:13.010: E/IOException(1676): at com.example.mycontactdetails.SendActivity$EventListTask1.doInBackground(SendActivity.java:311)
03-18 19:00:13.010: E/IOException(1676): at com.example.mycontactdetails.SendActivity$EventListTask1.doInBackground(SendActivity.java:1)
03-18 19:00:13.010: E/IOException(1676): at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-18 19:00:13.010: E/IOException(1676): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-18 19:00:13.010: E/IOException(1676): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-18 19:00:13.010: E/IOException(1676): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
03-18 19:00:13.010: E/IOException(1676): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
03-18 19:00:13.010: E/IOException(1676): at java.lang.Thread.run(Thread.java:1096)
03-18 19:00:13.090: E/AndroidRuntime(1676): FATAL EXCEPTION: main
03-18 19:00:13.090: E/AndroidRuntime(1676): java.lang.NullPointerException
03-18 19:00:13.090: E/AndroidRuntime(1676): at com.example.mycontactdetails.SendActivity$EventListTask1.onPostExecute(SendActivity.java:322)
03-18 19:00:13.090: E/AndroidRuntime(1676): at com.example.mycontactdetails.SendActivity$EventListTask1.onPostExecute(SendActivity.java:1)
03-18 19:00:13.090: E/AndroidRuntime(1676): at android.os.AsyncTask.finish(AsyncTask.java:417)
03-18 19:00:13.090: E/AndroidRuntime(1676): at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-18 19:00:13.090: E/AndroidRuntime(1676): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-18 19:00:13.090: E/AndroidRuntime(1676): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 19:00:13.090: E/AndroidRuntime(1676): at android.os.Looper.loop(Looper.java:123)
03-18 19:00:13.090: E/AndroidRuntime(1676): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-18 19:00:13.090: E/AndroidRuntime(1676): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 19:00:13.090: E/AndroidRuntime(1676): at java.lang.reflect.Method.invoke(Method.java:521)
03-18 19:00:13.090: E/AndroidRuntime(1676): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-18 19:00:13.090: E/AndroidRuntime(1676): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-18 19:00:13.090: E/AndroidRuntime(1676): at dalvik.system.NativeStart.main(Native Method)
我不明白出了什么问题?任何帮助将不胜感激。