0

私の友人と私は、カメラアプリを使用して写真を撮り、その写真を API にアップロードする Android アプリを作成しようとしています。私たちはどちらも Android をやったことがありませんが、私の友人は Java の経験があります。私はまったくの初心者です。カメラ部分とアップロード部分を別々に作成しましたが、どちらも機能しました。今、私の友人は 1 週間不在で、私は 2 つの部分を 1 つのアプリにまとめようとしています。残念ながら、アプリは起動時にクラッシュします。

編集: 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