首先,我尝试使用更改侦听器创建SeekBar。我希望我能正确地做到这一点。下面,red给我一个null异常。我相信这是因为我的列表是一个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):致命异常:主
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):在android.widget.TabHost $ IntentContentStrategy.getContentView(TabHost.java:654)
03-15 00:01:52.040:E / AndroidRuntime(345):在android.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):at java.lang.reflect.Method.invokeNative(Native Method)
03-15 00:01:52.040:E / AndroidRuntime(345):at java.lang.reflect.Method.invoke(Method.java:507)
03-15 00:01:52.040:E / AndroidRuntime(345):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
03-15 00:01:52.040:E / AndroidRuntime(345):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-15 00:01:52.040:E / AndroidRuntime(345):at dalvik.system.NativeStart.main(Native Method)
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更多
答案 0 :(得分:0)
R.id.seekBarID是否引用R.layout.seekbarlist中的某些内容?如果没有,那么
SeekBar red = (SeekBar)findViewById(R.id.seekBarID);
将返回null,因为它找不到指定的视图,如果你也可以发布2个布局文件会很有帮助。