0

我和我的朋友一直在尝试制作一个使用相机应用程序拍照然后将该照片上传到 API 的 Android 应用程序。我们俩之前都没有做过Android,但是我的朋友对java有一些经验。我是一个完全的新手。我们分别构建了相机部分和上传部分,它们都可以工作。现在我的朋友离开了一个星期,我一直在尝试将这两个部分放在一个应用程序中。不幸的是,该应用程序现在在启动时崩溃。

编辑:将 TVS 移至 OnCreate。

这是我的代码:

public class Main extends Activity implements OnClickListener {
        //private static final String STATE_SCORE = null;
        //private static final String STATE_LEVEL = null;
        int mCurrentScore;
        int mCurrentLevel;
        InputStream is;
        static String message="notnull";
        TextView tv;
        TextView tvs;
        static int i=0;
        private static final int TAKE_PICTURE = 0;
        private Uri mUri;
        private Bitmap mPhoto;

        @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        subclass sub= new subclass();
        sub.execute();
         setContentView(R.layout.activity_main);
         ((Button) findViewById(R.id.cam)).setOnClickListener(this);
         tvs=(TextView)findViewById(R.id.textie);
        //if(i<0)
            tvs.setText(message);
     }

    @Override
    public void onClick(View view) {
            if (view.getId()== R.id.cam) {
                    Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
                    File f = new File(Environment.getExternalStorageDirectory(),  "photo.jpg");
                    i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                    mUri = Uri.fromFile(f);
                    startActivityForResult(i, TAKE_PICTURE);
            } 

        }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == Activity.RESULT_OK) {
                getContentResolver().notifyChange(mUri, null);
                 ContentResolver cr = getContentResolver();
                   try {
                       mPhoto = android.provider.MediaStore.Images.Media.getBitmap(cr, mUri);
                    ((ImageView)findViewById(R.id.photo_holder)).setImageBitmap(mPhoto);
                   } 
                   catch (Exception e) {
                        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
                      }
                 }
           }
/*  @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        tvs=(TextView)findViewById(R.id.textie);
        //if(i<0)
            tvs.setText(message);
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }*/
    public void start(View view){
        disp();


    }
    //display converted string in textView
    public void disp(){
        tv=(TextView)findViewById(R.id.textie);
        tv.setText(message);
        tv.invalidate();
        i++;
    }

    private class subclass extends AsyncTask<Object, Object, Object>{

        @Override
        protected Object doInBackground(Object... arg0) {
            //create bitmap
            Bitmap bitmapOrg = BitmapFactory.decodeFile("photo.jpg");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, baos);
            byte [] ba = baos.toByteArray();
            //encode bytes
            int flag = 0;
            String ba1 = Base64.encodeToString(ba, flag);           
            ArrayList<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("image",ba1));
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://url.placeholder.com");
            try{
                //upload bytes
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                //handle response
                HttpEntity entity = response.getEntity();
                is = (InputStream) entity.getContent(); 
                //convert to string
                message=convertStreamToString(is);
            }
            catch(Exception e){ 
                Log.e("log_tag", "Error in http connection "+e.toString());
            }

            return null;
        }
        public String convertStreamToString(java.io.InputStream is) {
            java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
            return s.hasNext() ? s.next() : "";
        }

    }
    }

我的错误日志说

07-30 11:50:13.531: W/dalvikvm(8865): threadid=11: thread exiting with uncaught exception (group=0x40a9ba08)
07-30 11:50:13.541: D/AndroidRuntime(8865): Shutting down VM
07-30 11:50:13.541: W/dalvikvm(8865): threadid=1: thread exiting with uncaught exception (group=0x40a9ba08)
07-30 11:50:13.541: E/AndroidRuntime(8865): FATAL EXCEPTION: AsyncTask #1
07-30 11:50:13.541: E/AndroidRuntime(8865): java.lang.RuntimeException: An error occured while executing doInBackground()
07-30 11:50:13.541: E/AndroidRuntime(8865):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
07-30 11:50:13.541: E/AndroidRuntime(8865):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-30 11:50:13.541: E/AndroidRuntime(8865):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-30 11:50:13.541: E/AndroidRuntime(8865):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-30 11:50:13.541: E/AndroidRuntime(8865):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-30 11:50:13.541: E/AndroidRuntime(8865):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
07-30 11:50:13.541: E/AndroidRuntime(8865):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-30 11:50:13.541: E/AndroidRuntime(8865):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-30 11:50:13.541: E/AndroidRuntime(8865):     at java.lang.Thread.run(Thread.java:864)
07-30 11:50:13.541: E/AndroidRuntime(8865): Caused by: java.lang.NullPointerException
07-30 11:50:13.541: E/AndroidRuntime(8865):     at com.example.cameraupload.Main$subclass.doInBackground(Main.java:118)
07-30 11:50:13.541: E/AndroidRuntime(8865):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
07-30 11:50:13.541: E/AndroidRuntime(8865):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-30 11:50:13.541: E/AndroidRuntime(8865):     ... 5 more
07-30 11:50:13.551: D/Process(8865): killProcess, pid=8865
07-30 11:50:13.551: D/Process(8865): dalvik.system.VMStack.getThreadStackTrace(Native Method)
07-30 11:50:13.551: D/Process(8865): java.lang.Thread.getStackTrace(Thread.java:599)
07-30 11:50:13.551: D/Process(8865): android.os.Process.killProcess(Process.java:807)
07-30 11:50:13.551: D/Process(8865): com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:104)
07-30 11:50:13.551: D/Process(8865): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
07-30 11:50:13.551: D/Process(8865): java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
4

0 回答 0