我正在尝试设置一个带有背景图像和滑块的视图
我还希望能够以编程方式更改图像,以便我可以附加侦听器并在特定点更改图像。我正处于这个阶段的早期阶段,我在建立我的观点时遇到了困难;
//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));
}
}
Hi Pratick,正如我在下面写的那样,我仍然收到错误。
12-06 13:02:34.074:E / AndroidRuntime(808):未捕获的处理程序:由于未捕获的异常,线程主退出
虽然我不认为它提供了很多信息。这是在我注释掉对 setContentView(mLayout) 的第二次调用之后。
我刚刚通过调试器运行它,它出错了 mSeekBar.setOnSeekBarChangeListener(this);
我的建议没有成功,并尝试了一种新策略 我现在正在尝试使用 LayoutInflater,但我在 AVD 上遇到了相同的错误,当我单步执行代码时,正如我认为我即将离开调用“inflater.inflate”的行时,它给出了错误“找不到源”,并在调试器代码窗格中打开一个窗口,允许我进行导航。 我已经在 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 开始的。
I also want to be able to change the image programmatically so that I can attach a listener and change the image at specific points. I'm at the early stages of this and I'm having trouble setting up the view I have;
//inside my activity
LinearLayout mLayout;
//then inside the oncreate
mLayout = (LinearLayout) findViewById(R.id.main1);
setContentView(mLayout);
mLayout.setBackgroundResource(R.drawable.pic);
This compiles fine in eclipse but when I run it the app fails to start.
I'm looking to create a slide show with music.
I have tried the solutions given below but I am still getting an error;
12-06 11:51:11.656: E/AndroidRuntime(238): Uncaught handler: thread main exiting due to uncaught exception
Ultimately I want a slide show that will allow me to set the background image programmatically so I'm trying to get a handle on a view so that I can set the background through a listener.
I have set up a view
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));
}
}
Hi Pratick as I wrote below I'm still getting an error.
12-06 13:02:34.074: E/AndroidRuntime(808): Uncaught handler: thread main exiting due to uncaught exception
Although I don't think it is very informative. This was after I commented out the second call to setContentView(mLayout).
I've just ran it through the debugger and its erred at
mSeekBar.setOnSeekBarChangeListener(this);
I had no success with the suggestions and have tried a new tactic
I'm now trying to use a LayoutInflater but I'm getting the same error on the AVD and when I step through the code Just as I think I'm about to leave the line that calls 'inflater.inflate' it gives the error "source not found" and brings up a window in the debugger code pane that allows me to navigate.
I've created the 2 views in xml and received no errros when I saved
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);
}
}
I'm wondering if this error and the previous error has nothing to do with the code and is more concerned with my set up. But I have created the project and AVD to run in version 3. I've looked up LayoutInflater and it says it's been about since version 1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有在
setContentView()
中设置任何布局并通过 ID 查找对象,它总是会返回 null。您需要首先设置布局,然后从该布局中获取对象 id
without setting any layout in
setContentView()
and find the object by it's ID it's always getting null.You need to first set the layout and then get object id from that layout
更改
原因
:
findViewById
适用于当前内容视图。在设置内容视图之前,findViewById
将始终返回 null。因此,您将在发布的最后一行收到 NullPointerException。Change
to
Reason:
findViewById
works on the current content view. Before you have set a content viewfindViewById
will always return null. You will therefore get a NullPointerException on the last line you posted.