回电课不工作

时间:2013-07-10 06:47:14

标签: android flashlight

我有一个应用程序,它是一个火炬。我添加了一项功能,如果用户没有设备中的闪光灯,那么它应该启动前屏幕手电筒。这是我的代码..

LightsOn.java

public class LightsOn extends Activity implements OnKeyListener
{
ImageView button;
Boolean flashon=true;
private boolean hasFlash;
Parameters myparas;
private Camera mycamera;

@Override
protected void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_light);


    hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

    button=(ImageView)findViewById(R.id.power);
    button.setBackgroundResource(R.drawable.offswitch);
    button.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            if(flashon)
            {
                button.setBackgroundResource(R.drawable.onswitch);
                if (mycamera == null || myparas == null) 
                    {
                        return;
                    }
                else
                    {
                    myparas = mycamera.getParameters();
                    myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
                    mycamera.setParameters(myparas);
                    mycamera.stopPreview();
                    flashon=false;
                    }
            }
            else
            {
                button.setBackgroundResource(R.drawable.offswitch);
                 if (mycamera == null || myparas == null) 
                    {
                        return;
                    }
                 else
                    {
                    myparas = mycamera.getParameters();
                    myparas.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    mycamera.setParameters(myparas);
                    mycamera.startPreview();
                    flashon=true;
                    }
            }
        }
    });

}

     private void getCamera() 
        {
            if (mycamera == null) 
            {
                try 
                {
                    mycamera = Camera.open();
                    myparas = mycamera.getParameters();
                } 
                catch (RuntimeException e)
                {
                    Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
                }
            }



    if (!hasFlash)
    {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("Sorry your device doesnt support flash. Do you want to use your front screen as torch.?");
        dialog.setMessage("Press 'OKAY' to exit..");
        dialog.setPositiveButton("Use screen", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) 
           {
               Intent intent = new Intent(LightsOn.this,FrontScreen.class);
               startActivity(intent);
           }
        });
        dialog.setNegativeButton("More Apps by AKSHAT JAISWAL", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) 
               {
                    String page="https://play.google.com/store/apps/developer?id=Akshat+Jaiswal#?t=W251bGwsbnVsbCxudWxsLDEsImFrc2hhdC5qYWlzd2FsLnNob3dtZXRoYXRhcHAiXQ..";
                    Intent newIntent= new Intent();
                    newIntent.setAction(Intent.ACTION_VIEW);
                    newIntent.setData(android.net.Uri.parse(page));
                    Uri.parse(page);
                    startActivity(newIntent);
                    finish();
                    moveTaskToBack(true);
               }
            });
        dialog.setNeutralButton("Exit", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) 
               {
                   finish();
                   moveTaskToBack(true);
               }
            });


        dialog.show();
    }


}



     @Override
        protected void onDestroy() {
            super.onDestroy();
        }

        @Override
        protected void onPause()
        {
            super.onPause();
        }

        @Override
        protected void onRestart() {
            super.onRestart();
        }

        @Override
        protected void onResume() {
            super.onResume();    

            if(hasFlash)
            myparas = mycamera.getParameters();
            myparas.setFlashMode(Parameters.FLASH_MODE_TORCH);
            mycamera.setParameters(myparas);
            mycamera.startPreview();
            flashon=true;
        }

         @Override
             protected void onStop()
         {
             super.onStop();
         }


         @Override
             protected void onStart() {
             super.onStart();
             // on starting the app get the camera params
             getCamera();
                }

 @Override
    public boolean onCreateOptionsMenu(Menu m)
    {
        MenuInflater inf=getMenuInflater(); 
        inf.inflate(R.menu.menu,m);
        return true;

    }

    public boolean onOptionsItemSelected(MenuItem itm)
    {

        if(itm.getItemId()==R.id.it1)
        {

            if(flashon)
            {
                button.setBackgroundResource(R.drawable.onswitch);
                if (mycamera == null || myparas == null) 
                    {
                        return false;
                    }
                else
                    {
                    myparas = mycamera.getParameters();
                    myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
                    mycamera.setParameters(myparas);
                    mycamera.stopPreview();
                    flashon=false;
                    }
            }
            else
            {
                button.setBackgroundResource(R.drawable.offswitch);
                 if (mycamera == null || myparas == null) 
                    {
                        return false;
                    }
                 else
                    {
                    myparas = mycamera.getParameters();
                    myparas.setFlashMode(Parameters.FLASH_MODE_TORCH);
                    mycamera.setParameters(myparas);
                    mycamera.startPreview();
                    flashon=true;
                    }
            }


        }


        else if(itm.getItemId()==R.id.it2)
        {
            Intent intent = new Intent(LightsOn.this,FrontScreen.class);
            startActivity(intent);
        }


        else if(itm.getItemId()==R.id.it3)
        {
            Toast.makeText(getApplicationContext(), "Website getting launched soon..!!", Toast.LENGTH_SHORT).show();
            //String page="http://www.akshatjaiswal.in";
            //Intent newIntent= new Intent();
            //newIntent.setAction(Intent.ACTION_VIEW);
            //newIntent.setData(android.net.Uri.parse(page));
            //Uri.parse(page);
            //startActivity(newIntent);
        }

        else if(itm.getItemId()==R.id.it4)
        {
            String page="https://play.google.com/store/apps/developer?id=Akshat+Jaiswal#?t=W251bGwsbnVsbCxudWxsLDEsImFrc2hhdC5qYWlzd2FsLnNob3dtZXRoYXRhcHAiXQ..";
            Intent newIntent= new Intent();
            newIntent.setAction(Intent.ACTION_VIEW);
            newIntent.setData(android.net.Uri.parse(page));
            Uri.parse(page);
            startActivity(newIntent);
        }

        else if(itm.getItemId()==R.id.it5)
        {

            openDialog();
        }       
        return true;    
        }

    private void openDialog(){
        final Dialog dialog = new Dialog(LightsOn.this);
        dialog.setTitle("Thank You..");
        dialog.setContentView(R.layout.dialoglayout);
        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
        Button btnDismiss = (Button)dialog.getWindow().findViewById(R.id.exit);
        Button btnCancel = (Button)dialog.getWindow().findViewById(R.id.cancel);


        btnDismiss.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                if(flashon)
                {
                    button.setBackgroundResource(R.drawable.onswitch);
                    if (mycamera == null || myparas == null) 
                        {
                            return;
                        }
                    else
                        {
                        myparas = mycamera.getParameters();
                        myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
                        mycamera.setParameters(myparas);
                        mycamera.stopPreview();
                        mycamera.release();
                        finish();
                        moveTaskToBack(true);
                        }
                }
                else
                {
                    myparas = mycamera.getParameters();
                    myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
                    mycamera.setParameters(myparas);
                    mycamera.stopPreview();
                    mycamera.release();
                finish();
               moveTaskToBack(true);
                }
            }});

        btnCancel.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }});

        dialog.show();
    }


    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onBackPressed() 
    {
        // TODO Auto-generated method stub
        super.onBackPressed();

        myparas = mycamera.getParameters();
        myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
        mycamera.setParameters(myparas);
        mycamera.stopPreview();
        flashon = false;

        if (mycamera != null) {
            mycamera.release();
            mycamera = null;
        }
        Log.d("Camera","Back Pressed");
    }

}

现在,如果用户没有闪光灯,则会启动此课程。

FrontScreen.java

public class FrontScreen extends Activity
{
Boolean torchon=false;
ImageView button;
RelativeLayout lyt;

@Override
protected void onCreate(Bundle savedInstanceState) 
{

    super.onCreate(savedInstanceState);
    setContentView(R.layout.frontscreen);

    lyt=(RelativeLayout)findViewById(R.id.layout);
    button=(ImageView)findViewById(R.id.power);
    button.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(torchon)
            {
            lyt.setBackgroundResource(R.drawable.background);
            torchon=false;
            }
            else
            {
            lyt.setBackgroundResource(R.drawable.fronttorch);
            torchon=true;
            }

        }
    });



}


     @Override
        public boolean onCreateOptionsMenu(Menu m)
        {
            MenuInflater inf=getMenuInflater(); 
            inf.inflate(R.menu.frontmenu,m);
            return true;

        }

        public boolean onOptionsItemSelected(MenuItem itm)
        {

            if(itm.getItemId()==R.id.it1)
            {

                if(torchon)
                {
                    lyt.setBackgroundResource(R.drawable.background);
                    torchon=false;
                }
                else
                {
                    lyt.setBackgroundResource(R.drawable.fronttorch);
                    torchon=true;
                }


            }


            **else if(itm.getItemId()==R.id.it2)
            {
                Intent intents = new Intent(FrontScreen.this,LightsOn.class);
                startActivity(intents);
            }**


            else if(itm.getItemId()==R.id.it3)
            {
                Toast.makeText(getApplicationContext(), "Website getting launched soon..!!", Toast.LENGTH_SHORT).show();

            }

            else if(itm.getItemId()==R.id.it4)
            {

            }

            else if(itm.getItemId()==R.id.it5)
            {

                openDialog();
            }       
            return true;    
            }

        private void openDialog(){
            final Dialog dialog = new Dialog(FrontScreen.this);
            dialog.setTitle("Thank You..");
            dialog.setContentView(R.layout.dialoglayout);
            dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
            Button btnDismiss = (Button)dialog.getWindow().findViewById(R.id.exit);
            Button btnCancel = (Button)dialog.getWindow().findViewById(R.id.cancel);
            btnDismiss.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v) 
                {
                    if(torchon)
                    {
                    lyt.setBackgroundResource(R.drawable.background);
                    }
                    else
                    {
                    finish();
                    moveTaskToBack(true);
                    }
                }});

            btnCancel.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }});

            dialog.show();
        }


}

现在当我从这个类中调用第一个类时,它在选项菜单项2中,显示出NullPointerException ..

这是我的logcat消息

 07-10 13:27:40.111: W/dalvikvm(17187): threadid=1: thread exiting with uncaught     exception (group=0x4104e258)
 07-10 13:27:40.115: E/AndroidRuntime(17187): FATAL EXCEPTION: main
 07-10 13:27:40.115: E/AndroidRuntime(17187): java.lang.RuntimeException: Unable to resume activity {akshat.jaiswal.showmethatapp/akshat.jaiswal.showmethatapp.LightsOn}:     java.lang.NullPointerException
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2595)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2623)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2109)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at android.app.ActivityThread.access$600(ActivityThread.java:134)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at android.os.Handler.dispatchMessage(Handler.java:99)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at android.os.Looper.loop(Looper.java:154)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at android.app.ActivityThread.main(ActivityThread.java:4624)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at java.lang.reflect.Method.invokeNative(Native Method)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at java.lang.reflect.Method.invoke(Method.java:511)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at dalvik.system.NativeStart.main(Native Method)
 07-10 13:27:40.115: E/AndroidRuntime(17187): Caused by: java.lang.NullPointerException
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at akshat.jaiswal.showmethatapp.LightsOn.onResume(LightsOn.java:169)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1159)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at android.app.Activity.performResume(Activity.java:4553)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2581)
 07-10 13:27:40.115: E/AndroidRuntime(17187):   ... 12 more

任何人都可以提前帮助我.. Thanx。

1 个答案:

答案 0 :(得分:0)

只是一个猜测。如果设置了onResume标志,则hasFlash中的代码似乎已执行。如果是这样,onResume应该是这样的

@Override
protected void onResume() {
    super.onResume();    

    if(hasFlash){
        myparas = mycamera.getParameters();
        myparas.setFlashMode(Parameters.FLASH_MODE_TORCH);
        mycamera.setParameters(myparas);
        mycamera.startPreview();
        flashon=true;
    }
}