我是android开发的初学者。我正在尝试制作一个将米转换为cm的转换器应用程序。但是在onClick方法中它没有从spinner获取所选项目。除了应用程序停止。
MainActivity.java
package com.example.spinnertext;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
Spinner sp;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=(Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter= ArrayAdapter.createFromResource(this, R.array.items, android.R.layout.simple_spinner_item);
sp.setAdapter(adapter);
sp.setOnItemSelectedListener(new CustomListener());
Button b=(Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
EditText et=(EditText) findViewById(R.id.editText1);
sp=(Spinner) findViewById(R.id.spinner1);
Double d = null,s=Double.valueOf(et.getText().toString());
if(arg0.getId()==R.id.spinner1)
{
Log.d("TEST", "From Main");
if(sp.getSelectedItemPosition()==0)
{
d=s*100;
Log.d("TEST", "Meter From Main");
}
else
{
Log.d("TEST", "Cm From Main");
d=s;
}
}
Toast.makeText(arg0.getContext(), d.toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
CustomListener.java
package com.example.spinnertext;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
public class CustomListener implements OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Log.d("TEST", arg0.getSelectedItem().toString()+"selected");
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
布局文件:
<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"
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:layout_marginTop="31dp"
android:ems="10" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/editText1"
android:layout_toRightOf="@+id/editText1" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="42dp"
android:textSize="25sp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="47dp"
android:text="Button" />
</RelativeLayout>
LogCat显示以下错误:
03-24 13:22:05.357: D/libEGL(1505): loaded /system/lib/egl/libEGL_genymotion.so
03-24 13:22:05.361: D/(1505): HostConnection::get() New Host Connection established 0xb89c08e8, tid 1505
03-24 13:22:05.385: D/libEGL(1505): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
03-24 13:22:05.385: D/libEGL(1505): loaded /system/lib/egl/libGLESv2_genymotion.so
03-24 13:22:05.465: W/EGL_genymotion(1505): eglSurfaceAttrib not implemented
03-24 13:22:05.465: E/OpenGLRenderer(1505): Getting MAX_TEXTURE_SIZE from GradienCache
03-24 13:22:05.497: E/OpenGLRenderer(1505): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
03-24 13:22:05.497: D/OpenGLRenderer(1505): Enabling debug mode 0
03-24 13:22:05.525: D/TEST(1505): Meterselected
03-24 13:22:10.481: D/AndroidRuntime(1505): Shutting down VM
03-24 13:22:10.485: W/dalvikvm(1505): threadid=1: thread exiting with uncaught exception (group=0xa4bd5648)
03-24 13:22:10.489: E/AndroidRuntime(1505): FATAL EXCEPTION: main
03-24 13:22:10.489: E/AndroidRuntime(1505): java.lang.NullPointerException
03-24 13:22:10.489: E/AndroidRuntime(1505): at com.example.spinnertext.MainActivity$1.onClick(MainActivity.java:50)
03-24 13:22:10.489: E/AndroidRuntime(1505): at android.view.View.performClick(View.java:4240)
03-24 13:22:10.489: E/AndroidRuntime(1505): at android.view.View$PerformClick.run(View.java:17721)
03-24 13:22:10.489: E/AndroidRuntime(1505): at android.os.Handler.handleCallback(Handler.java:730)
03-24 13:22:10.489: E/AndroidRuntime(1505): at android.os.Handler.dispatchMessage(Handler.java:92)
03-24 13:22:10.489: E/AndroidRuntime(1505): at android.os.Looper.loop(Looper.java:137)
03-24 13:22:10.489: E/AndroidRuntime(1505): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-24 13:22:10.489: E/AndroidRuntime(1505): at java.lang.reflect.Method.invokeNative(Native Method)
03-24 13:22:10.489: E/AndroidRuntime(1505): at java.lang.reflect.Method.invoke(Method.java:525)
03-24 13:22:10.489: E/AndroidRuntime(1505): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-24 13:22:10.489: E/AndroidRuntime(1505): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-24 13:22:10.489: E/AndroidRuntime(1505): at dalvik.system.NativeStart.main(Native Method)
来自CustomListener类的LogCat显示消息,但不显示OnClick方法日志。
答案 0 :(得分:0)
你的吐司中有一个空指针异常。
Toast.makeText(arg0.getContext(), d.toString(), Toast.LENGTH_SHORT).show();
可能是arg0 == null或d == null。
检查这两个值pls
答案 1 :(得分:0)
arg0.getId()==R.id.spinner1
将始终返回false
,因为arg0
是对您按钮的引用。这意味着您的d
引用永远不会被初始化(您只在if
子句中执行),因此调用d.toString()
会导致NullPointerException。
答案 2 :(得分:0)
取出:
et=(EditText) findViewById(R.id.editText1);
sp=(Spinner) findViewById(R.id.spinner1);
从onClick
到onCreate()
并将它们声明为最终的全局变量:
private final EditText et;
private final Sinner sp;
然后您可以在onClick()
希望这有帮助!