为什么在绘制活动屏幕之前关闭此力? (机器人)

时间:2010-06-29 21:37:47

标签: android

我的应用程序在启动之前保持强制关闭... eclipse在我的代码中没有返回错误,我的xml很好。任何人都可以给我一些见解吗?

package com.mhe.test.scan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button myScanButton = (Button) findViewById(R.id.myScanButton);         
          myScanButton.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
                startActivityForResult(intent, 0);

               }
            });
    }


    EditText totalbox = (EditText) findViewById(R.id.totalbox);
        public void onActivityResult(int requestCode, int resultCode, Intent intent) {
            if (requestCode == 0) {
                if (resultCode == RESULT_OK) {
                    String contents = intent.getStringExtra("SCAN_RESULT");
                    // Handle successful scan
                    totalbox.setText(contents);
                } else if (resultCode == RESULT_CANCELED) {
                    // Handle cancel
                    totalbox.setText("@string/bummer");
                }



            }      
         }
    }

本质上它应该调用zxing条形码扫描器扫描按钮点击上的条形码并将结果返回到EditText字段...我是一个完全新手...

1 个答案:

答案 0 :(得分:1)

该行:

EditText totalbox = (EditText) findViewById(R.id.totalbox);

不在OnCreate方法中,所以它试图在创建活动之前初始化totalbox,可能导致崩溃

请改为尝试:

package com.mhe.test.scan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class main extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button myScanButton = (Button) findViewById(R.id.myScanButton); 
    // set the totalbox in the onCreate
    totalbox = (EditText) findViewById(R.id.totalbox);        
    myScanButton.setOnClickListener(new Button.OnClickListener() {
      public void onClick(View v) {
        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
        intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
        startActivityForResult(intent, 0);
      }
    });
  }

  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
      if (resultCode == RESULT_OK) {
        String contents = intent.getStringExtra("SCAN_RESULT");
        // Handle successful scan
        // check that it exists, just in case
        if ( totalbox != null )
          totalbox.setText(contents);
      } else if (resultCode == RESULT_CANCELED) {
        // Handle cancel
        // check that it exists, just in case
        if ( totalbox != null )
          totalbox.setText("@string/bummer");
      }
    }      
  }

  private EditText totalbox;
}