私は完全な初心者プログラマーです(Android版)。私はAndroidでListenerを作成するという概念に混乱しています。私のプログラムの目的は簡単です:ユニット間の変換(華氏から摂氏、摂氏から華氏へなど)。 EditTextオブジェクトのテキストを取得するにはどうしたらいいですか?私は実行するたびにエラーが発生します(残念ながら、Converter.exeは停止しました。)これまでのところ私のコードです。お知らせ下さい。EditTextから華氏温度を処理し、対応する摂氏温度を表示するにはどうすればいいですか?
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ConverterActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.temp_Button);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
setContentView(R.layout.body);
}
});
Button ok = (Button) findViewById(R.id.OK_Button);
ok.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
EditText et = (EditText) getText(0);
final int num = Integer.parseInt(et.toString());
double answer = (num - 32) * 5/9;
TextView tv = (TextView) findViewById(R.id.editText);
tv.setText(Double.toString(answer));
}
});
}
}
2つのレイアウトファイル(.xml)もあります(適切なプログラミングスタイルであるかどうかはわかりません)。
mainは、単にクリックできるボタンを表示します。次に、ボタンをクリックすると、2番目の.xmlファイルが表示されます。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/temp_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/F" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/C" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/I" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Cm" />
</LinearLayout>
body.xml(1秒) -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Enter temperature:" />
<EditText
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@android:drawable/editbox_background"
android:digits="1, 2, 3, 4, 5, 6, 7, 8, 9" />
<Button
android:id="@+id/cancel_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
<Button
android:id="@+id/OK_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
</LinearLayout>
私が言ったように、私は何か間違ったことをやっている場合、私は、私と一緒にその裸の初心者です。また、ちょうど私の問題は、コードのこのセグメントで発生し、あらためて表明します
Button ok = (Button) findViewById(R.id.OK_Button);
ok.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
EditText et = (EditText) getText(0);
final int num = Integer.parseInt(et.toString());
double answer = (num - 32) * 5/9;
TextView tv = (TextView) findViewById(R.id.editText);
tv.setText(Double.toString(answer));
}
});
意図は、ユーザ入力は、それを変換することのEditTextオブジェクトから番号を取得し、バックアウト、それを印刷することですが、私は」私がこの権利をしているのか、これが正しい場所にいてもわからない。
**更新:ここにログがあります。
02-06 19:53:36.148: D/AndroidRuntime(529): Shutting down VM
02-06 19:53:36.158: W/dalvikvm(529): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
02-06 19:53:36.168: E/AndroidRuntime(529): FATAL EXCEPTION: main
02-06 19:53:36.168: E/AndroidRuntime(529): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sapra.converter/com.sapra.converter.ConverterActivity}: java.lang.NullPointerException
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.os.Looper.loop(Looper.java:137)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-06 19:53:36.168: E/AndroidRuntime(529): at java.lang.reflect.Method.invokeNative(Native Method)
02-06 19:53:36.168: E/AndroidRuntime(529): at java.lang.reflect.Method.invoke(Method.java:511)
02-06 19:53:36.168: E/AndroidRuntime(529): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-06 19:53:36.168: E/AndroidRuntime(529): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-06 19:53:36.168: E/AndroidRuntime(529): at dalvik.system.NativeStart.main(Native Method)
02-06 19:53:36.168: E/AndroidRuntime(529): Caused by: java.lang.NullPointerException
02-06 19:53:36.168: E/AndroidRuntime(529): at com.sapra.converter.ConverterActivity.onCreate(ConverterActivity.java:29)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.Activity.performCreate(Activity.java:4465)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-06 19:53:36.168: E/AndroidRuntime(529): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-06 19:53:36.168: E/AndroidRuntime(529): ... 11 more
どのようなエラーがありますか?そして 'getText(0)'は正しくありません。 'getText()。toString()'を使ってください。 – adneal
aneal、私のコードが正しければ私の質問は尋ねていますか?あなたが言った間違いを訂正しました(ありがとう)。特に間違ったことがありますか? – sameetandpotatoes
ボタンをクリックするとどうなりますか?ログがありますか?それらは言及してアップロードするのに便利なものです。 – adneal