在此程序中获取空指针异常

时间:2015-09-29 17:56:02

标签: android

*这是编码和解码Image到Base64的代码,反之亦然* /

 package com.example.santosh.encode;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.support.v4.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageButton;
    import android.widget.TextView;
    import android.widget.Toast;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.IOException;
    import com.example.santosh.encode.Base64;
/**
 * A placeholder fragment containing a simple view.
 */
 public class FragmentBFragment extends Fragment {

  public FragmentBFragment() {
  }
  TextView textFile;
EditText editText;
Button clear,decode,send;
private  static  final int PICK_FILE=1;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = (View) inflater.inflate(R.layout.fragment_fragment_b, container, false);
    Button select = (Button) v.findViewById(R.id.button4);
    textFile = (TextView) v.findViewById(R.id.textView6);
    clear = (Button) v.findViewById(R.id.imageButton);
    editText = (EditText) v.findViewById(R.id.editText3);
    select.setOnClickListener(new View.OnClickListener() {
                                  @Override
                                  public void onClick(View v) {
                                      Intent intent = new  Intent(Intent.ACTION_GET_CONTENT);
                                      intent.setType("file/*");
                                      startActivityForResult(intent, PICK_FILE);
                                  }
                              }


    );
    clear.setOnClickListener(new View.OnClickListener() {   // this is working
        @Override
        public void onClick(View v) {

            Activity activity;
            activity = getActivity();
            editText.setText("");
            Toast toast = Toast.makeText(activity, "Text Has Been Cleaned", Toast.LENGTH_LONG);
            toast.show();

        }
    });

    send.setOnClickListener(new View.OnClickListener() { /* There is error NullPointer*/
        public void onClick(View v) {
            Intent sendIntent = new Intent();
            String send;
            send=editText.getText().toString();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, send);
            sendIntent.setType("text/plain");
            startActivity(sendIntent);

        }
    }



    );

    decode.setOnClickListener(new View.OnClickListener() { /*there is also same error*/
        @Override
        public void onClick(View v) {

            Activity activity;
            activity = getActivity();
            SharedPreferences pref =activity.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);

            pref.edit().putString("NAME", editText.getText().toString()).commit();

            Toast toast = Toast.makeText(activity, "Image for this Code", Toast.LENGTH_LONG);
            toast.show();

        }
    });


    return  v;
}

public void onActivityResult(int requestCode, int resultCode, Intent data)
{               /* this is also working*/
    super.onActivityResult(requestCode, resultCode, data);
    Bitmap bitmap = null;
    String Path=data.getData().getPath();
    textFile.setText(Path);
    if (resultCode == Activity.RESULT_OK)
    {
        Uri imageUri = data.getData();
        try {
             bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), imageUri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        if (bitmap != null) {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        }
        byte[] b = baos.toByteArray();
        String imageEncoded = Base64.encodeBytes(b);
        editText.setText(imageEncoded);
    }
}

}

0 个答案:

没有答案