2013-06-20 39 views
1

我正在嘗試從Android中的SeekBar中讀取東西,它位於DialogFragment中。它不斷崩潰。 SeekBar單獨出現沒有問題,但每當我嘗試重寫函數時,我都可以使用SeekBar來查找不斷崩潰的東西。我試圖複製基本上我見過其他人作爲工作代碼發佈的內容(儘管不在DialogFragment中),但它不起作用。有人能告訴我這裏有什麼問題嗎?SeekBar在DialogFragment中

numColumnsControl = (SeekBar) getActivity().findViewById(R.id.seekBar1); 
    numColumnsControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
    { 
     //@Override 
     public void onProgressChanged(SeekBar arg0, int progress, boolean fromTouch) 
     { 
      ((TextView) (getActivity().findViewById(R.id.textView2))).setText(String.valueOf(progress)); 

     } 

     //@Override 
     public void onStartTrackingTouch(SeekBar arg0) 
     { 
      // TODO Auto-generated method stub 

     } 

     //@Override 
     public void onStopTrackingTouch(SeekBar arg0) 
     { 
      // TODO Auto-generated method stub 

     } 
    });` 

每當我執行動作打開DialogFragment時,它都會崩潰。當我評論整個部分時,它很好。這個問題在某種程度上覆蓋了這些功能,即使它們是空的。當整個部分被評論時,View會出現SeekBars和TextViews。

我不認爲這很重要,但這裏是DialogFragment的XML。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:id="@+id/titleText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:text="Settings" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView2" 
     android:layout_below="@+id/seekBar1" 
     android:layout_marginTop="20dp" 
     android:text="NumShifts" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <SeekBar 
     android:id="@+id/seekBar1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView2" 
     android:gravity="center" 
     android:layout_gravity="center" 
     android:max="15" 
     android:progress="6" 
     android:secondaryProgress="0"/> 

    <SeekBar 
     android:id="@+id/SeekBar01" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView3" 
     android:gravity="center" 
     android:layout_gravity="center" 
     android:max="9" 
     android:progress="3" 
     android:secondaryProgress="0"/> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/titleText" 
     android:layout_below="@+id/titleText" 
     android:layout_marginTop="18dp" 
     android:text="NumColumns" 
     android:textAppearance="?android:attr/textAppearanceSmall" /> 

</RelativeLayout> 

</LinearLayout> 

下面是從類中的整個代碼:

package com.example.[I have to hide this name]; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.Dialog; 
import android.app.DialogFragment; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.widget.LinearLayout; 
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 
import android.widget.TextView; 

public class SettingsDialogueFragment extends DialogFragment 
{ 
private int numColums; 
private int numShifts; 
private Context ctx; 
private SeekBar numColumnsControl = null; 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) 
{ 
    ctx = getActivity(); 
    // Use the Builder class for convenient dialog construction 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    LayoutInflater inflater= getActivity().getLayoutInflater(); 


    builder.setView(inflater.inflate(R.layout.settings_menu, null)) 
     .setPositiveButton("DONE", new DialogInterface.OnClickListener() 
     { 
      public void onClick(DialogInterface dialog, int id) 
      { 
       //done 
      } 
     }) 
     .setNegativeButton("CANCEL", new DialogInterface.OnClickListener() 
     { 
      public void onClick(DialogInterface dialog, int id) 
      { 
       // User cancelled the dialog 
      } 
     }); 
    // Create the AlertDialog object and return it 
    LinearLayout settings = new LinearLayout(ctx); 

    numColumnsControl = (SeekBar) getActivity().findViewById(R.id.seekBar1); 
    Activity myActivity = getActivity(); 
    if(myActivity == null) 
    { 
     Log.e("bill", "bill"); 
    } 
    if(numColumnsControl == null) 
    { 
     Log.e("smith", "smith"); 
    } 
    numColumnsControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
    { 
     //@Override 
     public void onProgressChanged(SeekBar arg0, int progress, boolean fromTouch) 
     { 
      ((TextView) (getActivity().findViewById(R.id.textView2))).setText(String.valueOf(progress)); 
      Log.e("asd", "asd"); 

     } 

     //@Override 
     public void onStartTrackingTouch(SeekBar arg0) 
     { 
      Log.e("asdf", "asdf"); 
      // TODO Auto-generated method stub 

     } 

     //@Override 
     public void onStopTrackingTouch(SeekBar arg0) 
     { 
      Log.e("asdfg", "asdfg"); 
      // TODO Auto-generated method stub 

     } 
    }); 

    return builder.create(); 
} 
} 
+0

你可以發佈錯誤的痕跡嗎? –

+0

錯誤是一個NullPointerException,它發生在第51行,這是我給你的片段中的第二行(「numColumnsControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener()」) – user2471612

+0

因此錯誤與numColumnsControl有關。該片段? –

回答

0

基於您的評論

「的錯誤是一個NullPointerException,其中發生在51行是 第二我給你的片段中的行 (「numColumnsControl.setOnSeekBarChangeListener(new OnSeekBarChangeListener()」)'

檢查findViewById(R.id.Seekbar1)是否不返回null。如果確實如此,請確保在請求佈局中的任何視圖元素之前,您的對話框佈局已充滿。 也可能出現R引用錯誤(嘗試項目 - >在eclipse中清理)。

編輯我不知道,如果它的工作原理,但儘量在此改變onViewCreated(),並在這裏把你findViewById和setOnSeekBarChangeListener代碼。

以某種方式編輯 onViewCreated()未調用(Android DialogFragment onViewCreated not called)。這使得解決方案更加困難。您需要首先對視圖進行充氣,然後在充氣視圖上執行findViewById。

View menuView = inflater.inflate(R.layout.settings_menu); 
numColumnsControl = (SeekBar) menuView.findViewById(R.id.seekBar1, null); 
numColumnsControl.setOnSeekBarChangeListener ... 
builder.setView(menuView); 
+0

問題是第二個巴斯說,我已經誇大了佈局,但清理項目沒有做任何事情,我將編輯頂部帖子,包括所有的代碼 – user2471612

+0

我已經包括一個可能你可以檢查它是否有效嗎? – Bas

+1

Hey Bas,當我這樣做時它不再崩潰,但onViewCreated似乎並沒有發生過,DialogFragment出現了,但是我在onViewCreated中放了一個日誌,它永遠不會發生。爲什麼會發生這種情況? – user2471612