班级不会写入我的SD卡

时间:2015-02-07 01:32:26

标签: android button android-fragments android-viewpager

package com.adamschardt.hauling;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends FragmentActivity {

    private static final int NUM_PAGES = 5;

    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;

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

        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new MyFragmentStatePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
    }

    @Override
    public void onBackPressed() {
        if (mPager.getCurrentItem() == 0) {
            super.onBackPressed();
        } else {
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    }

    private class MyFragmentStatePagerAdapter extends FragmentStatePagerAdapter {
        public MyFragmentStatePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
         Fragment tmpFragment;
         if(position==4){
          tmpFragment = new SlidePageSupportFragment4();
         }else{
          tmpFragment = new SlidePageSupportFragment();
             ((SlidePageSupportFragment)tmpFragment).setPageNumber(position);
         }
            return tmpFragment;
        }

        @Override
        public int getCount() {
            return NUM_PAGES;
        }
    }

      EditText fcontent,fcontent2,fcontent3,fcontent4,fname;
      Button write;
      protected void onCreate2(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.slide_page);
        fname = (EditText)findViewById(R.id.fname);
        fcontent = (EditText)findViewById(R.id.edit);
        fcontent2 = (EditText)findViewById(R.id.edit2);
        fcontent3 = (EditText)findViewById(R.id.edit3);
        fcontent4 = (EditText)findViewById(R.id.edit4);
        write = (Button)findViewById(R.id.btnwrite);

            write.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
          // TODO Auto-generated method stub
        String filename = fname.getText().toString();
        String filecontent = fcontent.getText().toString();
        String filecontent2 = fcontent2.getText().toString();
        String filecontent3 = fcontent3.getText().toString();
        String filecontent4 = fcontent4.getText().toString();

        FileOperations fop = new FileOperations();
        fop.write(filename, filecontent, filecontent2, filecontent3, filecontent4, filecontent4);
        if(fop.write(filename, filecontent, filecontent2, filecontent3, filecontent4, filecontent4)){
        Toast.makeText(getApplicationContext(), filename+".txt created", Toast.LENGTH_SHORT).show();
        }else{
          Toast.makeText(getApplicationContext(), "I/O error", Toast.LENGTH_SHORT).show();
        }
        }
      });


      }
      }

我不能写信给我的SD卡。事实上,写按钮不会做任何事情。我是否正确启动了它?我的代码错了吗?我的logcat也没有任何帮助:/任何帮助将不胜感激

我不确定是不是因为我正在使用查看寻呼机?

2 个答案:

答案 0 :(得分:0)

您是否为您的清单添加了SD权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

答案 1 :(得分:0)

之前我已经完成了SD卡处理,这并非易事。从Environment class处的基础知识开始。一个好的Stackoverflow是[获取外部SD位置] [2]

[2]:Get External SDCard Location in Android

我的问题的一个例子是三星手机,SD位于“/ storage / extSdCard /”。因此可能存在制造商的问题。

祝你好运......