Android打印机强制关闭单击关闭按钮时

时间:2017-04-04 07:40:45

标签: android

public class CropAgeDetailFragment extends Fragment {

    private static final String TAG = CropAgeDetailFragment.class.getName();
    private ImageView im;
    private SharedPreferences settings;
    private TextView tv;
    public static String nutritionRecommendation = "";

    private Map<Integer, Product> products = ShowCropsImages.cropMk.getCropAgeMks().get(AgeListFragment.selectedAgeMk).getAgeMk().getProductsMap();
    private Button mBVutton;
    private Button printsms;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

    {
        View view = inflater.inflate(R.layout.crop_age_detail_fragment, container, false);

        im = (ImageView) view.findViewById(R.id.im);
        tv = (TextView) view.findViewById(R.id.text);
        printsms = (Button)view.findViewById(R.id.sms);
        // mBVutton = (Button)view.findViewById(R.id.sms);
        ((Button) view.findViewById(R.id.more)).setText("MORE");//AppProperties._morebutton);
        ((Button) view.findViewById(R.id.video)).setText("VIDEO"+"");//AppProperties._videobutton);
        ((Button) view.findViewById(R.id.sms)).setText("PRINT"+"");//AppProperties._smsbutton);
        Log.i(TAG, "AgeListFragment.selectedAgeMk ="+AgeListFragment.selectedAgeMk);
        Log.i(TAG, "CropAgeListFragment.selectedproduct ="+CropAgeListFragment.selectedproduct);

//		Log.i(TAG, "videoPath="+HexaCropActivity.cropMK.getCropAgeMks().get(AgeListFragment.selectedAgeMk).getAgeMk().getProductsMap().get(CropAgeListFragment.selectedproduct).getVideos().toString());
//		HexaCropActivity.cropMK.getCropAgeMks().get(AgeListFragment.selectedAgeMk).getAgeMk().getProductsMap().get(CropAgeListFragment.selectedproduct).getVideos();

        view.findViewById(R.id.video).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                try {
                    ShowCropsImages.cropMk.getCropAgeMks().get(AgeListFragment.selectedAgeMk).getAgeMk()
                            .getProductsMap().get(CropAgeListFragment.selectedproduct).getVideos().toString();
                    if(ShowCropsImages.cropMk.getCropAgeMks().get(AgeListFragment.selectedAgeMk).getAgeMk()
                            .getProductsMap().get(CropAgeListFragment.selectedproduct).getVideos().size() <= 0){
                        Toast.makeText(getActivity(), "Videos are not available", Toast.LENGTH_SHORT).show();
                        return;
                    }

                }catch(Exception e){
                    Toast.makeText(getActivity(), "Select product", Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent i = new Intent(getActivity(),MkVideosGrid.class);
                getActivity().startActivity(i);
            }
        });

        view.findViewById(R.id.more).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                SharedPreferences settings = getActivity().getSharedPreferences(AppConstants.PREFS_SETTINGS, 0);

                final String language = settings.getString(AppConstants.LANGUAGE, "en");
                Log.i(TAG, "language = " + language);

                try {
                    if (language != "en") {
                        if(products.get(CropAgeListFragment.selectedproduct).getLocaleMorePdf().get(language)!=null){
                            Log.i(TAG,"products.get(CropAgeListFragment.selectedproduct).getLocaleMorePdf().get(language) = "
                                    + products.get(CropAgeListFragment.selectedproduct).getLocaleMorePdf()
                                    .get(language));
                            Pdfloader(products.get(CropAgeListFragment.selectedproduct).getLocaleMorePdf().get(language));
                        }else{
                            Log.i(TAG, "products.get(CropAgeListFragment.selectedproduct).getMorePdf() = "
                                    + products.get(CropAgeListFragment.selectedproduct).getMorePdf());
                            Pdfloader(products.get(CropAgeListFragment.selectedproduct).getMorePdf());
                        }
                    } else {
                        Log.i(TAG, "products.get(CropAgeListFragment.selectedproduct).getMorePdf() = "
                                + products.get(CropAgeListFragment.selectedproduct).getMorePdf());
                        Pdfloader(products.get(CropAgeListFragment.selectedproduct).getMorePdf());
                    }
                } catch (Exception e) {
                    Log.e(TAG, "exception is ="+e.getMessage());
                    if (CropAgeListFragment.selectedproduct == 0) {
                        Toast.makeText(getActivity(), "Select product", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(getActivity(), "Information not available", Toast.LENGTH_SHORT).show();
                    }

                }
            }
        });

        view.findViewById(R.id.sms).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                HomeActivity.data= "nutritionprint";
                //Toast.makeText(getActivity(), " Printer is not available", Toast.LENGTH_LONG).show();
                if (CropAgeListFragment.selectedproduct <= 0) {
                    Toast.makeText(getActivity(), "Select product", Toast.LENGTH_SHORT).show();
                    return;
                }
                im.setVisibility(View.GONE);
                tv.setVisibility(View.VISIBLE);
                SharedPreferences settings = getActivity().getSharedPreferences(AppConstants.PREFS_SETTINGS, 0);

                final String language = settings.getString(AppConstants.LANGUAGE, "en");
                Log.i(TAG, "language = " + language);
                try {
                    if (language != "en") {
                        Log.i(TAG, "products.get(CropAgeListFragment.selectedproduct).getLocaleSms().get(language) = "
                                + products.get(CropAgeListFragment.selectedproduct).getLocaleSms().get(language));
                        tv.setText(products.get(CropAgeListFragment.selectedproduct).getLocaleSms().get(language));
                        nutritionRecommendation = products.get(CropAgeListFragment.selectedproduct).getLocaleSms().get(language);
//						sendRequest(products.get(CropAgeListFragment.selectedproduct).getLocaleSms().get(language));

                    } else {

                        Log.i(TAG, "products.get(CropAgeListFragment.selectedproduct).getSms() = "
                                + products.get(CropAgeListFragment.selectedproduct).getSms());
                        tv.setText(products.get(CropAgeListFragment.selectedproduct).getSms());
                        nutritionRecommendation = products.get(CropAgeListFragment.selectedproduct).getSms();
//						sendRequest(products.get(CropAgeListFragment.selectedproduct).getSms());
                    }
                } catch (Exception e) {
                    Toast.makeText(getActivity(), "Select product", Toast.LENGTH_SHORT).show();
                }

                HomeActivity.printerPojo.setDoctorName(settings.getString("username", ""));
                HomeActivity.printerPojo.setDoctorPhoneNumber(settings.getString("phone", ""));
                HomeActivity.printerPojo.setCropName(SelectCrop.cropName);
                HomeActivity.printerPojo.setCropAge("");
                HomeActivity.printerPojo.setPestName("");
                HomeActivity.printerPojo.setEtlValue("");
                HomeActivity.printerPojo.setRecommentdation(tv.getText().toString());
                Log.i(TAG,"SMS RECOMMENDATION : " + HomeActivity.printerPojo.getRecommentdation());
                Intent smsNutrition = new Intent(getActivity(), InitialActivity.class);
                startActivity(smsNutrition);
            }


        });


      

        view.findViewById(R.id.arrow).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (SelectCrop.selectedNode == 1) {
                    IDTree.currentNodeBean.setNode(LiveObjs.live.getNode().getParent());
                    Intent i = new Intent(getActivity(), SelectCrop.class);
                    getActivity().startActivity(i);
                } else if (SelectCrop.selectedNode == 3) {
                    IDTree.currentNodeBean.setNode(LiveObjs.live.getNode().getParent());
                    Intent i = new Intent(getActivity(), SelectCrop.class);
                    getActivity().startActivity(i);
                }



            }
        });
        return view;

    }


    public void setImage(String string) {
        Bitmap img = BitmapFactory.decodeFile(string);
        im.setVisibility(View.VISIBLE);

        final ImageLoader imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(getActivity()));
        DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnFail(R.drawable.dummy).imageScaleType(ImageScaleType.EXACTLY).postProcessor(new BitmapProcessor() {
            @Override
            public Bitmap process(Bitmap bitmap) {
               // im.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                return Bitmap.createScaledBitmap(bitmap, 450, 400, false);
            }
        }).build();
        im.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageLoader.displayImage("file://"+string, im,options);

        tv.setVisibility(View.GONE);
//		im.setImageBitmap(img);

        im.setOnClickListener( new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // Log.i(TAG,
                // "products.get(CropAgeListFragment.selectedproduct).getPdf() =
                // "
                // +products.get(CropAgeListFragment.selectedproduct).getPdf());
                // Pdfloader(products.get(CropAgeListFragment.selectedproduct).getPdf());

                SharedPreferences settings = getActivity().getSharedPreferences(AppConstants.PREFS_SETTINGS, 0);

                final String language = settings.getString(AppConstants.LANGUAGE, "en");
                Log.i(TAG, "language = " + language);
                try {
                    if (language != "en") {
                        if(products.get(CropAgeListFragment.selectedproduct).getLocalePdf().get(language)!=null){
                            Log.i(TAG, "products.get(CropAgeListFragment.selectedproduct).getLocalePdf().get(language) = "
                                    + products.get(CropAgeListFragment.selectedproduct).getLocalePdf().get(language));
                            Pdfloader(products.get(CropAgeListFragment.selectedproduct).getLocalePdf().get(language));
                        }else{
                            Log.i(TAG, "products.get(CropAgeListFragment.selectedproduct).getPdf() = "
                                    + products.get(CropAgeListFragment.selectedproduct).getPdf());
                            Pdfloader(products.get(CropAgeListFragment.selectedproduct).getPdf());
                        }
                    } else {
                        Log.i(TAG, "products.get(CropAgeListFragment.selectedproduct).getPdf() = "
                                + products.get(CropAgeListFragment.selectedproduct).getPdf());
                        Pdfloader(products.get(CropAgeListFragment.selectedproduct).getPdf());
                    }
                } catch (Exception e) {
                    Toast.makeText(getActivity(), "Some thing wrong with PDF", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        });
    }

    public void Pdfloader(String pdfpath) {

        File file = new File(pdfpath);
        Log.i(TAG, "file1 =" + file.getAbsolutePath());
        Log.i(TAG, "is file =" + file.isFile());
        if (file.exists()) {
            Log.i(TAG, "if");
            if (file.getAbsolutePath().endsWith(".pdf")) {
                Uri path = Uri.fromFile(file);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                // Intent intent = new Intent(getActivity(),PdfFragment.class);
                intent.setDataAndType(path, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                try {
                    getActivity().startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(getActivity(), "No Application Available to View PDF", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(getActivity(), "There is no supported multiImagesListfile format available",
                        Toast.LENGTH_SHORT).show();
            }
        } else {
            Log.i(TAG, "else");
            Toast.makeText(getActivity(), "PDF file is not available", Toast.LENGTH_SHORT).show();
        }
    }



 

}

我在波纹管代码中显示的是我的活动是打印机当我点击打印机点击关闭按钮后打印它的显示力关闭请给我建议..它只在第一次打印时发生

package com.tene.products.uasr.activities;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.tene.platform.language.AppProperties;
import com.tene.platform.utils.AppConstants;
import com.tene.platform.utils.CommonUtils;
import com.tene.products.esap.service.params.ScreenMaster;
import com.tene.products.uasr.model.ScreenMasterManager;

import java.util.Map;

import tene.esap.state.LiveObjs;
import tene.split.util.mk.Product;

/**
 * Created by prashant on 27/3/17.
 */

public class CropAgeMkActivity extends Activity implements CropAgeListFragment.Communicator {
    // private boolean processClick = false;
    public static Button actionAudio;
    private SharedPreferences settings;
    private String audioFilePath;
   // final ScreenMaster scrnMaster = (new ScreenMasterManager(this)).getScreenMasterScreenId("SELECT_SYMPTOM");
    private long mLastClickTime = 0;
    private static final String TAG = AgeBio2MainActivity.class.getName();
    private ActionBar actionBar;
    int pos;
    private TextView mTitleage;
    private Button mPrintSms;
   // private Object mActivity;
    private Activity mActivity;

  // private Map<Integer, Product> products = ShowCropsImages.cropMk.getCropAgeMks().get(AgeListFragment.selectedAgeMk).getAgeMk().getProductsMap();


    @Override
    protected final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.crop_age_activity_main);
         mTitleage = (TextView)findViewById(R.id.head);
         mTitleage.setText(AppProperties._SSABI);

        findViewById(R.id.homebutton).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(CropAgeMkActivity.this);
                builder.setTitle("Are you sure,want to go to home? ").setMessage("Survey will be cancelled.").setIcon(android.R.drawable.ic_dialog_alert)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(getApplicationContext(),HomeActivity.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(intent);

                            }
                        })
                        .setNegativeButton("No", null)                        //Do nothing on no
                        .show();
            }
        });
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

      /*  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        View cView = getLayoutInflater().inflate(R.layout.actionbar_view, null);
        actionBar = getActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setCustomView(cView);
        actionBar.setIcon(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
        actionBar.show();

        TextView title = (TextView) cView.findViewById(R.id.action_title);
        title.setText("Tene");
        cView.findViewById(R.id.homebutton).setVisibility(View.INVISIBLE);
        cView.findViewById(R.id.homebutton).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i = new Intent(CropAgeMkActivity.this, HomeActivity.class);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        });
        // processClick = true;
        cView.findViewById(R.id.action_back).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (SystemClock.elapsedRealtime() - mLastClickTime < 2000) {
                    return;
                }
                mLastClickTime = SystemClock.elapsedRealtime();
                // if (processClick) {
                // processClick = false;
                onBackPressed();
            }
            // processClick=true;
            // }
        });

        actionAudio = (Button) cView.findViewById(R.id.action_audio);*/
       /* audioFilePath = CommonUtils.getAudioFilePath("IDTree.wav");
        if (scrnMaster.getScreenAudioExplainFile() != null) {
            audioFilePath = CommonUtils.getAudioFilePath(scrnMaster.getScreenAudioExplainFile().substring(0,
                    scrnMaster.getScreenAudioExplainFile().indexOf(".")) + "_" + LiveObjs.live.getSelectedLanguage()
                    + ".mp3");
        }

        Log.i(TAG, "audio file path  ==" + audioFilePath);

        if (!AppAudioPlayer.getInstance(getApplicationContext()).getAudioStatus()) {
            actionAudio.setVisibility(View.INVISIBLE);
        } else {
            AppAudioPlayer.getInstance(getApplicationContext()).startPlaying(audioFilePath);
        }

        actionAudio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AppAudioPlayer.getInstance(getApplicationContext()).startPlaying(audioFilePath);
            }
        });*/

    }

    @Override
    public void Message(String string) {
        CropAgeDetailFragment detailfragment = (CropAgeDetailFragment) getFragmentManager()
                .findFragmentById(R.id.crop_age_detail_Fragment);
        if (detailfragment != null && detailfragment.isInLayout()) {
            detailfragment.setImage(string);
        }
    }

    @Override
    public void onBackPressed() {
//      if (LiveObjs.live.getCurrentNode().getNode().getNodeType().equalsIgnoreCase("IDD")) {
//          LiveObjs.live.getCurrentNode().setNode(LiveObjs.live.getCurrentNode().getNode().getParent());
//          Intent i = new Intent(getApplicationContext(), SelectNode.class);
//          i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//          i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//          startActivity(i);
//      } else {
        CropAgeListFragment.selectedproduct = 0;
        Intent i = new Intent(getApplicationContext(), AgeBio2MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
//      }
    }

    @Override
    protected void onResume() {
        super.onResume();
        HomeActivity.currentActivity = 4;
        settings = getSharedPreferences(AppConstants.PREFS_SETTINGS, 0);
        String macId = settings.getString("printermacid", "NoPrinter");

        if (macId.equalsIgnoreCase("NoPrinter")) {
            findViewById(R.id.sms).setVisibility(View.INVISIBLE);
        } else {
            findViewById(R.id.sms).setVisibility(View.VISIBLE);
        }

    }


}

FATAL EXCEPTION: main
                                                                                   Process: com.tene.products.uasr.activities, PID: 20139
                                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tene.products.uasr.activities/com.tene.products.uasr.activities.CropAgeMkActivity}: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Map tene.split.util.mk.CropMk.getCropAgeMks()' on a null object reference
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614)
                                                                                       at android.app.ActivityThread.access$800(ActivityThread.java:178)
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                                       at android.os.Looper.loop(Looper.java:194)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5643)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                                    Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Map tene.split.util.mk.CropMk.getCropAgeMks()' on a null object reference
                                                                                       at com.tene.products.uasr.activities.CropAgeDetailFragment.<init>(CropAgeDetailFragment.java:60)
                                                                                       at java.lang.reflect.Constructor.newInstance(Native Method)
                                                                                       at java.lang.Class.newInstance(Class.java:1606)
                                                                                       at android.app.Fragment.instantiate(Fragment.java:622)
                                                                                       at android.app.FragmentState.instantiate(Fragment.java:109)
                                                                                       at android.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1801)
                                                                                       at android.app.Activity.onCreate(Activity.java:944)
                                                                                       at com.tene.products.uasr.activities.CropAgeMkActivity.onCreate(CropAgeMkActivity.java:53)
                                                                                       at android.app.Activity.performCreate(Activity.java:6100)
                                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2614) 
                                                                                       at android.app.ActivityThread.access$800(ActivityThread.java:178) 
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                                       at android.os.Looper.loop(Looper.java:194) 
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5643) 
                                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                                       at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960) 
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

1 个答案:

答案 0 :(得分:1)

应用程序在此行崩溃,

private Map<Integer, Product> products = ShowCropsImages.cropMk.getCropAgeMks().get(AgeListFragment.selectedAgeMk).getAgeMk().getProductsMap();

根据我的理解,ShowCropsImages.cropMk为空。