大家好,我正在尝试做一个单位转换器,但是当我运行它时它会停止并向我展示
05-15 15:30:45.237: D/AndroidRuntime(333): 关闭 VM 05-15 15:30:45.237: W/dalvikvm(333): threadid=1: 线程退出未捕获异常 (group=0x40015560) 05 -15 15:30:45.288:E/AndroidRuntime(333):致命异常:主要 05-15 15:30:45.288:E/AndroidRuntime(333):java.lang.RuntimeException:无法实例化活动 ComponentInfo{org.alaenvargut .conversor/org.alaenvargut.conversor.MainActivity}:java.lang.NullPointerException 05-15 15:30:45.288:E/AndroidRuntime(333):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569) 05- 15 15:30:45.288: E/AndroidRuntime(333): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 android.app。 ActivityThread.access$1500(ActivityThread.java:117) 05-15 15:30:45.288: E/AndroidRuntime(333):在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 android.os.Handler.dispatchMessage(Handler.java:99) 05- 15 15:30:45.288: E/AndroidRuntime(333): 在 android.os.Looper.loop(Looper.java:123) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 android.app。 ActivityThread.main(ActivityThread.java:3683) 05-15 15:30:45.288: E/AndroidRuntime(333): at java.lang.reflect.Method.invokeNative(Native Method) 05-15 15:30:45.288: E /AndroidRuntime(333): at java.lang.reflect.Method.invoke(Method.java:507) 05-15 15:30:45.288: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit$ MethodAndArgsCaller.run(ZygoteInit.java:839) 05-15 15:30:45.288: E/AndroidRuntime(333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 05-15 15: 30:45.288:E/Android 运行时(333):在 dalvik.system.NativeStart.main(Native Method) 05-15 15:30:45.288: E/AndroidRuntime(333): 引起:java.lang.NullPointerException 05-15 15:30:45.288: E/AndroidRuntime(333 ): 在 android.app.Activity.findViewById(Activity.java:1647) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 org.alaenvargut.conversor.MainActivity.(MainActivity.java:13) 05 -15 15:30:45.288: E/AndroidRuntime(333): 在 java.lang.Class.newInstanceImpl(Native Method) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 java.lang.Class。 newInstance(Class.java:1409) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1021) 05-15 15:30:45.288: E/ AndroidRuntime(333): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) 05-15 15:30:45.288: E/AndroidRuntime(333): ... 11 更多NativeStart.main(Native Method) 05-15 15:30:45.288: E/AndroidRuntime(333): 由: java.lang.NullPointerException 05-15 15:30:45.288: E/AndroidRuntime(333): at android. app.Activity.findViewById(Activity.java:1647) 05-15 15:30:45.288: E/AndroidRuntime(333): at org.alaenvargut.conversor.MainActivity.(MainActivity.java:13) 05-15 15:30 :45.288: E/AndroidRuntime(333): at java.lang.Class.newInstanceImpl(Native Method) 05-15 15:30:45.288: E/AndroidRuntime(333): at java.lang.Class.newInstance(Class.java :1409) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1021) 05-15 15:30:45.288: E/AndroidRuntime(333):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) 05-15 15:30:45.288: E/AndroidRuntime(333): ... 11 更多NativeStart.main(Native Method) 05-15 15:30:45.288: E/AndroidRuntime(333): 由: java.lang.NullPointerException 05-15 15:30:45.288: E/AndroidRuntime(333): at android. app.Activity.findViewById(Activity.java:1647) 05-15 15:30:45.288: E/AndroidRuntime(333): at org.alaenvargut.conversor.MainActivity.(MainActivity.java:13) 05-15 15:30 :45.288: E/AndroidRuntime(333): at java.lang.Class.newInstanceImpl(Native Method) 05-15 15:30:45.288: E/AndroidRuntime(333): at java.lang.Class.newInstance(Class.java :1409) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1021) 05-15 15:30:45.288: E/AndroidRuntime(333):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) 05-15 15:30:45.288: E/AndroidRuntime(333): ... 11 更多288: E/AndroidRuntime(333): 引起: java.lang.NullPointerException 05-15 15:30:45.288: E/AndroidRuntime(333): at android.app.Activity.findViewById(Activity.java:1647) 05- 15 15:30:45.288: E/AndroidRuntime(333): 在 org.alaenvargut.conversor.MainActivity.(MainActivity.java:13) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 java.lang .Class.newInstanceImpl(Native Method) 05-15 15:30:45.288: E/AndroidRuntime(333): at java.lang.Class.newInstance(Class.java:1409) 05-15 15:30:45.288: E/ AndroidRuntime(333): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1021) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561 ) 05-15 15:30:45.288: E/AndroidRuntime(333): ... 11 更多288: E/AndroidRuntime(333): 引起: java.lang.NullPointerException 05-15 15:30:45.288: E/AndroidRuntime(333): at android.app.Activity.findViewById(Activity.java:1647) 05- 15 15:30:45.288: E/AndroidRuntime(333): 在 org.alaenvargut.conversor.MainActivity.(MainActivity.java:13) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 java.lang .Class.newInstanceImpl(Native Method) 05-15 15:30:45.288: E/AndroidRuntime(333): at java.lang.Class.newInstance(Class.java:1409) 05-15 15:30:45.288: E/ AndroidRuntime(333): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1021) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561 ) 05-15 15:30:45.288: E/AndroidRuntime(333): ... 11 更多288: E/AndroidRuntime(333): 在 android.app.Activity.findViewById(Activity.java:1647) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 org.alaenvargut.conversor.MainActivity.( MainActivity.java:13) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 java.lang.Class.newInstanceImpl(Native Method) 05-15 15:30:45.288: E/AndroidRuntime(333):在 java.lang.Class.newInstance(Class.java:1409) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1021) 05-15 15 :30:45.288: E/AndroidRuntime(333): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) 05-15 15:30:45.288: E/AndroidRuntime(333): ... 还有 11 个288: E/AndroidRuntime(333): 在 android.app.Activity.findViewById(Activity.java:1647) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 org.alaenvargut.conversor.MainActivity.( MainActivity.java:13) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 java.lang.Class.newInstanceImpl(Native Method) 05-15 15:30:45.288: E/AndroidRuntime(333):在 java.lang.Class.newInstance(Class.java:1409) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1021) 05-15 15 :30:45.288: E/AndroidRuntime(333): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) 05-15 15:30:45.288: E/AndroidRuntime(333): ... 还有 11 个(MainActivity.java:13) 05-15 15:30:45.288: E/AndroidRuntime(333): at java.lang.Class.newInstanceImpl(Native Method) 05-15 15:30:45.288: E/AndroidRuntime(333) : 在 java.lang.Class.newInstance(Class.java:1409) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1021) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) 05-15 15:30:45.288: E/AndroidRuntime(333): ... 还有 11 个(MainActivity.java:13) 05-15 15:30:45.288: E/AndroidRuntime(333): at java.lang.Class.newInstanceImpl(Native Method) 05-15 15:30:45.288: E/AndroidRuntime(333) : 在 java.lang.Class.newInstance(Class.java:1409) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1021) 05-15 15:30:45.288: E/AndroidRuntime(333): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) 05-15 15:30:45.288: E/AndroidRuntime(333): ... 还有 11 个E/AndroidRuntime(333): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) 05-15 15:30:45.288: E/AndroidRuntime(333): ... 11 更多E/AndroidRuntime(333): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) 05-15 15:30:45.288: E/AndroidRuntime(333): ... 11 更多
这是我的主要代码
package org.alaenvargut.conversor;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends Activity implements OnItemSelectedListener{
public Spinner SUni = (Spinner) findViewById(R.id.spinner1);
public Spinner SFm = (Spinner) findViewById(R.id.spinner2);
public Spinner STo = (Spinner) findViewById(R.id.spinner3);
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter AUni = ArrayAdapter.createFromResource(this, R.array.Unidades, android.R.layout.simple_selectable_list_item);
AUni.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SUni.setAdapter(AUni);
SUni.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view,int pos, long id)
{
if (parent.getId() == R.id.spinner1)
{
switch((int)id)
{
case(1):
{
}
case(2):
{
ArrayAdapter AFm = ArrayAdapter.createFromResource(this, R.array.Velocidad, android.R.layout.simple_selectable_list_item);
AFm.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SFm.setAdapter(AFm);
}
default:
}
}
}
public void onNothingSelected(AdapterView<?> parent)
{
// Another interface callback
}
}
这是我的布局
<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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="@string/principal" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="15dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner1"
android:layout_below="@+id/spinner1"
android:layout_marginTop="18dp"
android:text="@string/fm"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner2"
android:layout_below="@+id/spinner2"
android:text="@string/to"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner3"
android:layout_below="@+id/spinner3"
android:layout_marginTop="30dp"
android:text="@string/con" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_alignRight="@+id/spinner3"
android:layout_marginRight="29dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
这是我的值中的 xml 文件
> <?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="Unidades">
<item >Volumen</item>
<item >Velocidad</item>
</string-array>
<string-array name="Velocidad">
<item >Km/h</item>
<item >M/h</item>
</string-array>
</resources>
关于例外的任何想法?