我还希望能够以编程方式更改图像,以便我可以附加一个监听器并在特定点更改图像。我正处于这个阶段的早期阶段,我无法设置我的观点;
//inside my activity
LinearLayout mLayout;
//then inside the oncreate
mLayout = (LinearLayout) findViewById(R.id.main1);
setContentView(mLayout);
mLayout.setBackgroundResource(R.drawable.pic);
这在eclipse中编译得很好,但是当我运行它时,应用程序无法启动。
我正在寻找一个带音乐的幻灯片。
我已经尝试过下面给出的解决方案,但我仍然遇到错误;
12-06 11:51:11.656:E / AndroidRuntime(238):未捕获的处理程序:由于未捕获的异常导致主线程退出
最终我想要一个幻灯片放映,这将允许我以编程方式设置背景图像,所以我试图获取视图的句柄,以便我可以通过监听器设置背景。
我已经设置了一个视图res / layout / 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'的行会给出错误“source not found”,并在调试器代码窗格中显示一个允许我导航的窗口。我在xml中创建了2个视图,并在保存时没有收到任何错误信息
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以来它已经存在。
没有在setContentView()
中设置任何布局并通过它的ID找到对象它总是变为null。
您需要先设置布局,然后从该布局中获取对象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
更改
mLayout = (LinearLayout) findViewById(R.id.main1);
setContentView(mLayout);
至
setContentView(R.layout.yourLayoutFile);
mLayout = (LinearLayout) findViewById(R.id.main1);
原因:findViewById
适用于当前内容视图。在设置内容视图之前,findViewById
将始终返回null。因此,您将在发布的最后一行获得NullPointerException。