2013-04-22 85 views
1

我有一個應用程序,我需要能夠從一個活動切換到下一個,但也有第一個活動有一個溫度轉換器,它使用一個按鈕來計算答案。此按鈕用於工作,但由於我已更改oncreate以允許活動之間切換工作,它已停止工作,現在只是關閉應用程序。第二個按鈕上的一個活動關閉Android應用程序下降

public class MainActivity extends Activity { 
    private EditText text; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button next = (Button) findViewById(R.id.Button01); 
     next.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent myIntent = new Intent(view.getContext(), SecondScreen.class); 
       startActivityForResult(myIntent, 0); 
      } 
     }); 
    } 

    // This method is called at button click because we assigned the name to the 
    // "OnClick property" of the button 
    public void onClick(View view) { 
     switch (view.getId()) { 
     case R.id.button1: 
      RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0); 
      RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1); 
      if (text.getText().length() == 0) { 
       Toast.makeText(this, "Please enter a valid number", Toast.LENGTH_LONG).show(); 
       return; 
      } 
      float inputValue = Float.parseFloat(text.getText().toString()); 
      if (celsiusButton.isChecked()) { 
       text.setText(String.valueOf(convertFahrenheitToCelsius(inputValue))); 
       celsiusButton.setChecked(false); 
       fahrenheitButton.setChecked(true); 
      } 
      else { 
       text.setText(String.valueOf(convertCelsiusToFahrenheit(inputValue))); 
       fahrenheitButton.setChecked(false); 
       celsiusButton.setChecked(true); 
      } 
      break; 
     } 
    } 

    // Converts to celsius 
    private float convertFahrenheitToCelsius(float fahrenheit) { 
     return ((fahrenheit - 32) * 5/9); 
    } 

    // Converts to fahrenheit 
    private float convertCelsiusToFahrenheit(float celsius) { 
     return ((celsius * 9)/5) + 32; 
    } 
} 

的logcats空的,但是這是在其它部分

[2013-04-22 14:45:24 - ddms] Can't bind to local 8888 for debugger 
    [2013-04-22 15:04:58 - Unexpected error while launching logcat. Try reselecting the        device.] device not found 
    com.android.ddmlib.AdbCommandRejectedException: device not found 
    at com.android.ddmlib.AdbHelper.setDevice(AdbHelper.java:752) 
    at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:373) 
    at com.android.ddmlib.Device.executeShellCommand(Device.java:462) 
    at com.android.ddmuilib.logcat.LogCatReceiver$1.run(LogCatReceiver.java:110) 
    at java.lang.Thread.run(Unknown Source) 
    [2013-04-22 15:07:18 - ddms] Can't bind to local 8888 for debugger 

    4-22 15:26:56.392: W/Trace(909): Unexpected value from nativeGetEnabledTags: 0 
    04-22 15:26:56.397: W/Trace(909): Unexpected value from nativeGetEnabledTags: 0 
    04-22 15:26:56.417: D/AndroidRuntime(909): Shutting down VM 
    04-22 15:26:56.417: W/dalvikvm(909): threadid=1: thread exiting with uncaught    exception (group=0x40a70930) 
    04-22 15:26:56.447: E/AndroidRuntime(909): FATAL EXCEPTION: main 
    04-22 15:26:56.447: E/AndroidRuntime(909): java.lang.IllegalStateException: Could not execute method of the activity 
    04-22 15:26:56.447: E/AndroidRuntime(909): at android.view.View$1.onClick(View.java:3597) 
    04-22 15:26:56.447: E/AndroidRuntime(909): at android.view.View.performClick(View.java:4202) 
    04-22 15:26:56.447: E/AndroidRuntime(909): at android.view.View$PerformClick.run(View.java:17340) 
    04-22 15:26:56.447: E/AndroidRuntime(909): at android.os.Handler.handleCallback(Handler.java:725) 
    04-22 15:26:56.447: E/AndroidRuntime(909): at android.os.Handler.dispatchMessage(Handler.java:92) 
    04-22 15:26:56.447: E/AndroidRuntime(909): at android.os.Looper.loop(Looper.java:137) 
    04-22 15:26:56.447: E/AndroidRuntime(909): at android.app.ActivityThread.main(ActivityThread.java:5039) 
    04-22 15:26:56.447: E/AndroidRuntime(909): at java.lang.reflect.Method.invokeNative(Native Method) 
    04-22 15:26:56.447: E/AndroidRuntime(909): at java.lang.reflect.Method.invoke(Method.java:511) 
    04-22 15:26:56.447: E/AndroidRuntime(909): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    04-22 15:26:56.447: E/AndroidRuntime(909): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    04-22 15:26:56.447: E/AndroidRuntime(909): at dalvik.system.NativeStart.main(Native Method) 
    04-22 15:26:56.447: E/AndroidRuntime(909): Caused by: java.lang.reflect.InvocationTargetException 
    04-22 15:26:56.447: E/AndroidRuntime(909): at java.lang.reflect.Method.invokeNative(Native Method) 
    04-22 15:26:56.447: E/AndroidRuntime(909): at java.lang.reflect.Method.invoke(Method.java:511) 
    04-22 15:26:56.447: E/AndroidRuntime(909): at android.view.View$1.onClick(View.java:3592) 
    04-22 15:26:56.447: E/AndroidRuntime(909): ... 11 more 
    04-22 15:26:56.447: E/AndroidRuntime(909): Caused by: java.lang.NullPointerException 
    04-22 15:26:56.447: E/AndroidRuntime(909): at com.example.assignment2project.MainActivity.tempConverterClick(MainActivity.java:42) 
    04-22 15:26:56.447: E/AndroidRuntime(909): ... 14 more 
+0

post logcat info – Raghunandan 2013-04-22 14:02:18

+0

那是什麼?對不起新的日食 – 2013-04-22 14:03:50

+0

窗口 - >顯示查看 - >其他 - > Logcat – 2013-04-22 14:05:19

回答

1

我認爲是有衝突的,它不會出現你要重寫,而不是通用的點法onClick方法,所以我假設你已經定義XML中的onClick財產onClick,讓我們嘗試以下操作:

在XML中,定義onClick的東西像什麼我在下面:tempConverterClick

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="tempConverterClick" 
    android:text="@string/my_button_txt" /> 

現在在Java端,讓我們先來測試Toast

public void tempConverterClick(View view) { 
    Toast.makeText(this, "Temp Converter click worked!", Toast.LENGTH_LONG).show(); 
} 

如果這樣的作品,然後讓我們嘗試您最初的代碼在這個新的單擊處理:

public void tempConverterClick(View view) { 
    switch (view.getId()) { 
    case R.id.button1: 
     RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0); 
     RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1); 
     if (text.getText().length() == 0) { 
      Toast.makeText(this, "Please enter a valid number", Toast.LENGTH_LONG).show(); 
      return; 
     } 
     float inputValue = Float.parseFloat(text.getText().toString()); 
     if (celsiusButton.isChecked()) { 
      text.setText(String.valueOf(convertFahrenheitToCelsius(inputValue))); 
      celsiusButton.setChecked(false); 
      fahrenheitButton.setChecked(true); 
     } 
     else { 
      text.setText(String.valueOf(convertCelsiusToFahrenheit(inputValue))); 
      fahrenheitButton.setChecked(false); 
      celsiusButton.setChecked(true); 
     } 
     break; 
    } 
} 

爲了避免將來出現混亂和那些誰試圖幫助的混亂,我會強烈建議改變你的命名約定。創建ID的,涉及到的動作,目的等。具有R.id.Button01R.id.button1非常混亂


編輯

看來,當你改變了你的onCreate你可能已經刪除了EditText實例。

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Add this line with the appropriate ID reference 
    text = (EditText)findViewById(R.id.your_edit_text_id); 

    Button next = (Button) findViewById(R.id.Button01); 
    next.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      Intent myIntent = new Intent(view.getContext(), SecondScreen.class); 
      startActivityForResult(myIntent, 0); 
     } 
    }); 
} 
+0

當點擊計算按鈕(button1)時出現以下消息「不幸assign2項目已停止」,並關閉應用程序 – 2013-04-22 14:42:43

+0

public void tempConverterClick(View view){ Toast.makeText(this,「Temp Converter click worked!」,Toast .LENGTH_LONG).show(); }這工作,但是當我們用新的處理程序使用原始代碼時,它關閉了應用程序 – 2013-04-22 14:48:10

+0

您是否通過上面顯示的代碼獲得此代碼?該消息應該有一個'LogCat'錯誤,你能告訴我那個嗎? – jnthnjns 2013-04-22 14:48:25

0

限定外onCreate方法您的開關殼體方法並用的OnCreate中的onclick方法中的參數調用它。它像 checkbuttonclick(buttonid);

你的checkbuttonclick(int id)。將包含開關盒的方法定義。

相關問題