2014-07-24 95 views
0

我是一名初學Android開發人員,我的代碼中有一個運行時錯誤:當我想在模擬器或設備中運行它時,它顯示「強制關閉」按摩。我的日誌是在這裏:Android運行時錯誤

http://upir.ir/934/1_5e07a.jpg

我的Java代碼:

public class Third extends Activity 

{ 


protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    Button d = (Button) findViewById(R.id.btn4); 

    d.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        dialog(); 
       } 
    }); 
} 

public void dialog(){ 


    final Dialog dialog = new Dialog(Third.this); 
    dialog.setContentView(R.layout.dialog); 
    dialog.setTitle("واحد عدد وارد شده را انتخاب کنید"); 
    dialog.show(); 
    RadioGroup rg = (RadioGroup) dialog.findViewById(R.id.rg); 

    final RadioButton rb1 = (RadioButton) dialog.findViewById(R.id.rb1); 
    final RadioButton rb2 = (RadioButton) dialog.findViewById(R.id.rb2); 
    rg.setOnCheckedChangeListener(new OnCheckedChangeListener() { 


     @Override 
     public void onCheckedChanged(RadioGroup arg0, int arg1) { 
      if(rb1.isChecked()) { 
       dialog.dismiss(); 
       Button t = (Button)findViewById(R.id.btn5); 
       t.setOnClickListener(new OnClickListener() { 
        public void onClick(View arg0) { 
        EditText ft1 = (EditText)findViewById(R.id.f3); 
    TextView foot = (TextView)findViewById(R.id.foot); 
    TextView mile = (TextView)findViewById(R.id.mile); 
    TextView inch = (TextView)findViewById(R.id.inch); 
    TextView yard = (TextView)findViewById(R.id.yard); 
    TextView mm = (TextView)findViewById(R.id.millymeter); 
    TextView dm = (TextView)findViewById(R.id.decimeter); 
    TextView mim = (TextView)findViewById(R.id.micrometer); 
    TextView nm = (TextView)findViewById(R.id.nanometer); 
    TextView hand = (TextView)findViewById(R.id.hand); 
    TextView iron = (TextView)findViewById(R.id.iron); 
    TextView point = (TextView)findViewById(R.id.point); 
    if(ft1.getText().toString().length() == 0){return;} 
    int first = Integer.parseInt(ft1.getText().toString()); 
    double equal = first *0.0328; 
    DecimalFormat formatf = new DecimalFormat("#.####"); 
    String x = formatf.format(equal)+" فوت"; 
    foot.setText(x); 
    first = Integer.parseInt(ft1.getText().toString()); 
    equal = first * 0.000005; 
    DecimalFormat formatm = new DecimalFormat("#.####"); 
    x = formatm .format(equal)+"مایل"; 
    mile.setText(x); 
    equal = first * 0.393; 
    DecimalFormat formati = new DecimalFormat("#.####"); 
    x = formati.format(equal)+"اینچ"; 
    inch.setText(x); 
    equal = first * 0.0109; 
    DecimalFormat formaty = new DecimalFormat("#.#####"); 
    x = formaty.format(equal)+"یارد"; 
    yard.setText(x); 
    equal = first/10; 
    DecimalFormat formatmi = new DecimalFormat("#.##"); 
    x = formatmi.format(equal)+"دسی متر"; 
    dm.setText(x); 
    int equalmm = first * 10; 
    x = equalmm+"میلی متر"; 
    mm.setText(x); 
    int equalm = first * 10000; 
    x = equalm+"میکرو متر"; 
    mim.setText(x); 
    int equaln = first * 10000000; 
    x = equaln + "نانو متر"; 
    nm.setText(x); 
    equal = first * 0.098; 
    DecimalFormat formath = new DecimalFormat("#####.#####"); 
    x = formath.format(equal)+"هَند"; 
    hand.setText(x); 
    equal = first * 19; 
    x = equal+"آیرون"; 
    iron.setText(x); 
    equal = first * 28; 
    x = equal+"پوینت"; 
    point.setText(x); 
} 

     }); 
     } 

回答

0

你需要先用一個佈局資源,以虛增您的活動之前,你可以使用findViewById()方法來檢索意見。如果沒有佈局,那麼沒有可能找到的意見。

+0

我有很多像按鈕或編輯文本的小工具,但它不工作,然後當我刪除該按鈕代碼它去活動,但不顯示任何東西 – Vahid

+0

謝謝你,我解決它,爲你知道Eclips,它不接受沒有任何錯誤或紅線的可能文本視圖的ID – Vahid

0

您缺少一個重要部分。您需要爲您的Activity添加視圖,以便查找您要嘗試實例化的視圖元素,如Button

在你OnCreate方法補充一點:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.the_xml_file); //Here add the xml file with the view you want to show 
    Button d = (Button) findViewById(R.id.btn4); 

    d.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
        dialog(); 
       } 
      }); 
} 
+0

我做了你的建議,但它沒有工作,我在我的活動中有一些東西。 – Vahid

+0

「我的活動中的某些東西」是什麼意思? –

+0

我的意思是我有一個像編輯文本或文本視圖或按鈕的視圖 – Vahid

0

您super.onCreate(savedInstanceState)之後;你在設置內容視圖嗎?

例如

setContentView(R.layout.nameofxmlfilehere);

+0

還有,您是否已將活動添加到您的AndroidManifest文件中? – coderoligist

+0

謝謝,我解決了它,就像你知道Eclips一樣,它不接受沒有任何錯誤或紅線的可能文本視圖的ID – Vahid