我是一个相当新的编程,我试图编写一个小应用程序来练习,看看我是否可以让它工作。不幸的是,每当我尝试在模拟器上运行我的应用程序时,我会收到错误消息,说它被迫意外停止,我该怎么办?在我的logcat中,main中有一个致命的错误,我附上了。
感谢您的帮助
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:gravity="center|center_vertical|center_horizontal" android:clickable="true" android:layout_height="wrap_content">
<TextView android:textSize="25dp" android:layout_width="wrap_content" android:id="@+id/textView1" android:layout_height="wrap_content" android:text="@string/Title"></TextView>
<Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:prompt="@string/SpinPrompt" android:entries="@array/Amountballs"></Spinner>
<Button android:layout_width="fill_parent" android:id="@+id/button1" android:layout_height="wrap_content" android:text="@string/Pick"></Button>
<TextView android:text="TextView" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
Java文件...... 包darby.android.lotpicker;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class Pick extends Activity implements android.view.View.OnClickListener, AdapterView.OnItemSelectedListener{
TextView selection;
Spinner spinner1;
Button button1;
TextView textview2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner1 = (Spinner)findViewById(R.id.spinner1);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
spinner1.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this,R.array.Amountballs , android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
}
public class MyOnItemSelectedListener implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> parent, View v, int pos, long id ) {
spinner1.getSelectedItem();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
@Override
public void onClick(View v) {
int index = 0;
int amount = (Integer) spinner1.getSelectedItem();
while(index<amount){
CharSequence yournumber = (nextInt(49+1));
textview2.setText(yournumber);
index ++;
Log.d("Pick","Numbers Picked");
}
}
private CharSequence nextInt(int i) {
return null;
}
@Override
public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
LogCat错误
12-10 19:06:30.500: E/AndroidRuntime(510): FATAL EXCEPTION: main
12-10 19:06:30.500: E/AndroidRuntime(510): java.lang.NullPointerException
12-10 19:06:30.500: E/AndroidRuntime(510): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.view.View.measure(View.java:8313)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.view.View.measure(View.java:8313)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.view.View.measure(View.java:8313)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.view.View.measure(View.java:8313)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.view.View.measure(View.java:8313)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.os.Handler.dispatchMessage(Handler.java:99)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.os.Looper.loop(Looper.java:123)
12-10 19:06:30.500: E/AndroidRuntime(510): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-10 19:06:30.500: E/AndroidRuntime(510): at java.lang.reflect.Method.invokeNative(Native Method)
12-10 19:06:30.500: E/AndroidRuntime(510): at java.lang.reflect.Method.invoke(Method.java:507)
12-10 19:06:30.500: E/AndroidRuntime(510): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-10 19:06:30.500: E/AndroidRuntime(510): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-10 19:06:30.500: E/AndroidRuntime(510): at dalvik.system.NativeStart.main(Native Method)