Сначала я пытаюсь создать SeekBar с прослушивателем изменений. Я надеюсь, что я делаю это правильно. Ниже красный дает мне нулевое исключение. Я полагаю, что это потому, что мой список - один xml, а мой компоновщик поисковой панели / textview - другой xml. Это тот случай? Как бы я поймал это?
seekbars.xml
<?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:layout_height="wrap_content"
android:id="@+id/seekBarLayout"
>
<TextView
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/seekBarLabel"/>
<TextView
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/seekBarValue"/>
<SeekBar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/seekBarID"/>
</LinearLayout>
seekbarlist.xml
<?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:layout_height="wrap_content">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
код Android
package thisPackage.Tabs;
public class ColorsActivity extends ListActivity implements SeekBar.OnSeekBarChangeListener {
/** Called when the activity is first created. */
//Array Adapter that will hold our ArrayList and display the items on the ListView
SeekBarAdaptor seekBarAdaptor;
//List that will host our items and allow us to modify that array adapter
ArrayList<SeekBar> seekBarArrayList=null;
TextView myValueText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seekbarlist);
//Initialize ListView
ListView lstTest= getListView();
//Initialize our ArrayList
seekBarArrayList = new ArrayList<SeekBar>();
//Initialize our array adapter
seekBarAdaptor = new SeekBarAdaptor(ColorsActivity.this, R.layout.seekbars,seekBarArrayList);
//setContentView(R.layout.seekbars);
SeekBar red = (SeekBar)findViewById(R.id.seekBarID);
red.setOnSeekBarChangeListener(this);
// SeekBar blue = new SeekBar(this);
//Set the above adapter as the adapter of choice for our list
lstTest.setAdapter(seekBarAdaptor);
seekBarArrayList.add(red);
//seekBarArrayList.add(blue);
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
//myLayout = (LinearLayout)seekBar.findViewById(R.id.seekBarLayout);
myValueText = (TextView)findViewById(R.id.seekBarValue);
myValueText.setText("hello");
//myValueText.setText("text");
}
public void onStopTrackingTouch(SeekBar seekBar){
}
public void onStartTrackingTouch (SeekBar seekBar){
}
}
LogCat
03-15 00: 01: 52.020: D / AndroidRuntime (345): выключение виртуальной машины
03-15 00: 01: 52.020: W / dalvikvm (345): threadid = 1: поток завершается с необработанным исключением (группа = 0x40015560)
03-15 00: 01: 52.040: E / AndroidRuntime (345): ИСКЛЮЧИТЕЛЬНОЕ ИСКЛЮЧЕНИЕ: main
03-15 00: 01: 52.040: E / AndroidRuntime (345): java.lang.RuntimeException: невозможно запустить действие ComponentInfo {thisPackage.Tabs / thisPackage.Tabs.ColorsActivity}: java.lang.NullPointerException
03-15 00: 01: 52.040: E / AndroidRuntime (345): на android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1647)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на android.app.ActivityThread.startActivityNow (ActivityThread.java:1487)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на android.app.LocalActivityManager.moveToState (LocalActivityManager.java:127)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на android.app.LocalActivityManager.startActivity (LocalActivityManager.java:339)
03-15 00: 01: 52.040: E / AndroidRuntime (345): at android.widget.TabHost $ IntentContentStrategy.getContentView (TabHost.java:654)
03-15 00: 01: 52.040: E / AndroidRuntime (345): atroid.widget.TabHost.setCurrentTab (TabHost.java:326)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на android.widget.TabHost $ 2.onTabSelectionChanged (TabHost.java:132)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на android.widget.TabWidget $ TabClickListener.onClick (TabWidget.java:456)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на android.view.View.performClick (View.java:2485)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на android.view.View $ PerformClick.run (View.java:9080)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на android.os.Handler.handleCallback (Handler.java:587)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на android.os.Handler.dispatchMessage (Handler.java:92)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на android.os.Looper.loop (Looper.java:123)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на android.app.ActivityThread.main (ActivityThread.java:3683)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на java.lang.reflect.Method.invokeNative (собственный метод)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на java.lang.reflect.Method.invoke (Method.java:507)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:839)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на com.android.internal.os.ZygoteInit.main (ZygoteInit.java:597)
03-15 00: 01: 52.040: E / AndroidRuntime (345): в dalvik.system.NativeStart.main (собственный метод)
03-15 00: 01: 52.040: E / AndroidRuntime (345): вызвано: java.lang.NullPointerException
03-15 00: 01: 52.040: E / AndroidRuntime (345): at thisPackage.Tabs.ColorsActivity.onCreate (ColorsActivity.java:52)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1047)
03-15 00: 01: 52.040: E / AndroidRuntime (345): на android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1611)
03-15 00: 01: 52.040: E / AndroidRuntime (345): ... еще 18