gpt4 book ai didi

android - 我的开关 (view.getId()) 不工作

转载 作者:太空狗 更新时间:2023-10-29 16:38:38 24 4
gpt4 key购买 nike

我正在尝试做一个练习,这意味着我需要使用按钮从 imageView 更改图像。整个事情被实现到练习的另一个项目中。

问题是:在第二种情况下它甚至没有打开 Toast“测试”。

package de.vogella.android.temperature;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends Activity{
private EditText text;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText) findViewById(R.id.editText1);
Button button1 = (Button) findViewById(R.id.tempbutton);
Button button2 = (Button) findViewById(R.id.imageconverter);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tempbutton:
button1Click();
break;

case R.id.imageconverter:

button2Click();
break;
}
}
};
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);

}




public void button1Click (){
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(ConverterUtil.convertFahrenheitToCelsius(inputValue)));
celsiusButton.setChecked(false);
fahrenheitButton.setChecked(true);
} else {
text.setText(String
.valueOf(ConverterUtil.convertCelsiusToFahrenheit(inputValue)));
fahrenheitButton.setChecked(false);
celsiusButton.setChecked(true);
}
}

public void button2Click (){
ImageView frontImageView = (ImageView) findViewById(R.id.myicon);
frontImageView.setImageResource(R.drawable.assigned);
Toast.makeText(this,"done", Toast.LENGTH_LONG).show();
}
}

XML 文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@color/myColor"
tools:context=".MainActivity" >

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:inputType="numberSigned|numberDecimal"
android:ems="10" >

<requestFocus />
</EditText>

<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1" >

<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/celsius" />

<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fahrenheit" />
</RadioGroup>

<Button
android:id="@+id/tempbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup1"
android:layout_below="@+id/radioGroup1"
android:text="@string/calc"
android:onClick="onClick" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_alignParentBottom="true"
android:text="@string/author"
android:textAppearance="?android:attr/textAppearanceSmall" />

<DigitalClock
android:id="@+id/digitalClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_alignLeft="@+id/textView1"
android:layout_alignRight="@+id/button1"
android:text="DigitalClock" />

<Button
android:id="@+id/imageconverter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:onClick="onClick"
android:text="Change Image" />

<ImageView
android:id="@+id/myicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:src="@drawable/initial" />

</RelativeLayout>

日志:

02-25 16:03:00.228: E/AndroidRuntime(1363):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.os.Handler.dispatchMessage(Handler.java:102)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.os.Looper.loop(Looper.java:136)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-25 16:03:00.228: E/AndroidRuntime(1363): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 16:03:00.228: E/AndroidRuntime(1363): at java.lang.reflect.Method.invoke(Method.java:515)
02-25 16:03:00.228: E/AndroidRuntime(1363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-25 16:03:00.228: E/AndroidRuntime(1363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-25 16:03:00.228: E/AndroidRuntime(1363): at dalvik.system.NativeStart.main(Native Method)
02-25 16:03:00.228: E/AndroidRuntime(1363): Caused by: java.lang.NullPointerException
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.app.Activity.findViewById(Activity.java:1884)
02-25 16:03:00.228: E/AndroidRuntime(1363): at de.vogella.android.temperature.MainActivity.<init>(MainActivity.java:24)
02-25 16:03:00.228: E/AndroidRuntime(1363): at java.lang.Class.newInstanceImpl(Native Method)
02-25 16:03:00.228: E/AndroidRuntime(1363): at java.lang.Class.newInstance(Class.java:1208)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
02-25 16:03:00.228: E/AndroidRuntime(1363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
02-25 16:03:00.228: E/AndroidRuntime(1363): ... 11 more

最佳答案

你煮错了。您应该找到您的按钮,然后附加监听器:

Button button1 = (Button) findViewById(R.id.tempbutton);
Button button2 = (Button) findViewById(R.id.imageconverter);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tempbutton:
button1Click();
break;

case R.id.imageconverter:
Toast.makeText(this, "test",Toast.LENGTH_LONG).show();
button2Click();
break;
}
}
};
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);

关于android - 我的开关 (view.getId()) 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22025498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com