2011-12-06 98 views
2

我也希望能夠以編程方式更改圖像,以便我可以附加偵聽器並在特定點更改圖像。我處於這個早期階段,而且我很難設定我的觀點。我試圖設置一個背景圖像和滑塊的視圖

//inside my activity 

LinearLayout mLayout; 

//then inside the oncreate 

mLayout = (LinearLayout) findViewById(R.id.main1); 
setContentView(mLayout); 
mLayout.setBackgroundResource(R.drawable.pic); 

這在eclipse中編譯得很好,但是當我運行它時,應用程序無法啓動。

我期待創建一個音樂幻燈片放映。

我已經嘗試了下面給出的解決方案,但我仍然收到錯誤;

11月12日至6日:51:116.56:E/AndroidRuntime(238):未捕獲的處理程序:螺紋主力退出,由於未捕獲的異常

最後我希望有一個幻燈片,讓我以編程方式設置背景圖片所以我試圖獲得一個視圖的句柄,以便我可以通過一個監聽器設置背景。

我已成立了一個視圖 RES /佈局/ slider.xml

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

    <SeekBar android:id="@+id/seek" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:max="100" 
     android:progress="50" 
     android:secondaryProgress="75" /> 

    <TextView android:id="@+id/progress" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <TextView android:id="@+id/tracking" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

SeekBar mSeekBar; 
TextView mProgressText; 
TextView mTrackingText; 
LinearLayout mLayout; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mSeekBar = (SeekBar)findViewById(R.id.seek); 
    mSeekBar.setOnSeekBarChangeListener(this); 
    mProgressText = (TextView)findViewById(R.id.progress); 
    mTrackingText = (TextView)findViewById(R.id.tracking); 


    setContentView(R.layout.slider); 
    mLayout = (LinearLayout) findViewById(R.id.main2); 
    setContentView(mLayout); 
    mLayout.setBackgroundResource(R.drawable.mumbai); 

    } 

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) { 
      mProgressText.setText(progress + " " + 
        getString(R.string.seekbar_from_touch) + "=" + fromTouch); 
      //mLayout = (LinearLayout) findViewById(R.id.main1); 
      //mLayout.setBackgroundResource(R.drawable.boats); 
    } 

     public void onStartTrackingTouch(SeekBar seekBar) { 
      mTrackingText.setText(getString(R.string.seekbar_tracking_on)); 
     } 

     public void onStopTrackingTouch(SeekBar seekBar) { 
      mTrackingText.setText(getString(R.string.seekbar_tracking_off)); 
     } 
} 

Pratick嗨,我寫了下面,我仍然得到一個錯誤。

12-06 13:02:34.074:E/AndroidRuntime(808):未捕獲的處理程序:螺紋主力退出,由於未捕獲的異常

雖然我不認爲這是非常豐富的。這是在我第二次調用setContentView(mLayout)之後註釋掉的。

我剛剛通過調試器運行它,它的錯誤在 mSeekBar.setOnSeekBarChangeListener(this);

我沒有成功的建議,並嘗試了一種新的策略 我現在試圖使用LayoutInflater,但我在AVD上遇到同樣的錯誤,並且當我單步執行代碼時就像我想的那樣如果要離開調用'inflater.inflate'的行,它會給出錯誤「未找到源」,並在調試器代碼窗格中顯示一個窗口,該窗口允許我導航。 我創建XML中的2次,並沒有收到errros當我救

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class PwdDialogActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     LayoutInflater inflater = (LayoutInflater) getSystemService 
      (Context.LAYOUT_INFLATER_SERVICE); 
     final View layout = inflater.inflate(R.layout.password_dialog, 
       (ViewGroup)findViewById(R.id.root)); 
     //layout.setBackgroundResource(R.drawable.boats); 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setView(layout); 
     builder.setTitle(R.string.app_name); 
    } 
} 

我想知道如果這個錯誤和以前的錯誤無關的代碼和更關心的是我的設置。但我已經創建項目和AVD在第3版上運行,我擡頭一看LayoutInflater和它說,它已經從大約1版

+0

[Rü得到任何錯誤得到對象ID? – Shruti

+0

嗨是的 - 12-06 13:02:34.074:E/AndroidRuntime(808):未捕獲的處理程序:線程主要由於未捕獲的異常退出 - 但我不認爲它非常翔實。這個錯誤是我在第二次調用setContentView(mLayout)時給出的,因爲@Pratik建議 – Martin

回答

1

沒有在setContentView()中設置任何佈局,並通過它的ID找到對象,它總是變爲空。

您需要首先設置佈局,然後從佈局

setContentView(R.layout.yourlayout); // here set the layout where object or control where defined it 
mLayout = (LinearLayout) findViewById(R.id.main1); 
setContentView(mLayout); // and then you can set that object 
+0

感謝Pratik和@JSydow我試圖讓這個工作,但我不認爲我的視圖設置正確。我有主要視圖設置與id main1這有它的seekbar控制器,但我不認爲這是正確的。 – Martin

+0

我創建了一個名爲main的新視圖,它未經編輯。我使用id main2將main1重命名爲res/layout/slider.xml。我的代碼現在是 setContentView(R.layout.main); mLayout =(LinearLayout)findViewById(R.id.main2); setContentView(mLayout); 對於多次更新感到抱歉,我無法在不使用返回 – Martin

+0

的情況下放入代碼,只是刪除最後一個setContentView()或發表評論。我認爲你只是想改變當前佈局的背景,那麼即使你在佈局文件的主要LinearLayout上設置了背景,也不需要使用這個方法 – Pratik

0

變化

mLayout = (LinearLayout) findViewById(R.id.main1); 
setContentView(mLayout); 

setContentView(R.layout.yourLayoutFile); 
mLayout = (LinearLayout) findViewById(R.id.main1); 

原因:findViewById適用於當前內容視圖。在設置內容視圖之前,findViewById將始終返回null。因此,您將在發佈的最後一行中得到一個NullPointerException。