2011-12-06 16 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。