2012-03-14 47 views
0

首先,我正在嘗試使用更改偵聽器創建SeekBar。我希望我正確地做到這一點。下面,紅色給我一個空例外。我相信這是因爲我的列表是一個XML,我的seekbar/textview佈局是另一個XML。是這樣嗎?我將如何去抓住它?Android SeekBar問題

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> 

機器人代碼

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):線程ID = 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):在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):at android.view.View $ PerformClick.run(View.java:9080)

03-15 00:01:52.040:E/AndroidRuntime(345):at android.os.Handler。 handleCallback(Handler.java:587)

03-15 00:01:52.040:E/AndroidRuntime(345):at 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):at android.app.ActivityThread.main(ActivityThread.java:3683)

03-15 00:01:52.040:E/AndroidRuntime(345):at java.lang.reflect .Method.invokeNative(本機方法)

03-15 00:01:52.040:E/AndroidRuntime(345):在java.lang.reflect.Method.invoke(Method.java:507)

月3日至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):在達爾維克。 system.NativeStart.main(本機方法)

03-15 00:01:52.040:E/AndroidRuntime(345):通過引起:顯示java.lang.NullPointerException

03-15 00:01:52.040: E/AndroidRuntime(345):在thisPackage.Tabs.ColorsActivity.onCreate(ColorsActivi ty.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):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

03-15 00:01:52.040:E/AndroidRuntime(345):... 18更多

+0

訪問空值時發生空指針異常。因此請檢查紅色值是否爲空。請發佈logcat。 – deepa 2012-03-14 04:35:34

+0

輸入完整的對話 – 2012-03-14 06:34:03

回答

0

R.id.seekBarID是指R.layout.seekbarlist中的內容嗎?如果沒有,那麼

SeekBar red = (SeekBar)findViewById(R.id.seekBarID); 

將返回null,因爲它無法找到指定的觀點,這將是有益的,如果你能發佈2頁佈局的文件。

+0

是的。這是我所設想的。我附加了XML文件。 – Spectrem 2012-03-15 00:41:36

+0

您還沒有在seekbarlist.xml中包含seekbars.xml,因爲您調用了setContentView(R.layout.seekbarlist);在您的代碼中沒有這樣做。這意味着seekbars.xml中的_nothing_已被添加到內容中。您應該在您的seekbarlist.xml文件中將添加到適當的位置(由您希望顯示的位置定義) – Kai 2012-03-15 02:16:00