gpt4 book ai didi

android - 我正在尝试设置一个带有背景图片和 slider 的 View

转载 作者:行者123 更新时间:2023-11-29 02:09:17 28 4
gpt4 key购买 nike

我还希望能够以编程方式更改图像,以便我可以附加一个监听器并在特定点更改图像。我正处于早期阶段,在设置我的 View 时遇到了问题;

//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): Uncaught handler: thread main exiting due to uncaught exception

最终,我想要一个允许我以编程方式设置背景图像的幻灯片放映,因此我试图获取 View 的句柄,以便我可以通过监听器设置背景。

我已经设置了 View 资源/布局/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): Uncaught handler: thread main exiting due to uncaught exception

虽然我不认为它的信息量很大。这是在我注释掉对 setContentView(mLayout) 的第二次调用之后。

我刚刚通过调试器运行了它,但在 mSeekBar.setOnSeekBarChangeListener(this);

我的建议没有成功,尝试了一种新策略我现在正在尝试使用 LayoutInflater,但我在 AVD 上遇到了同样的错误,当我单步执行代码时就像我想我要离开调用 'inflater.inflate' 的行一样,它给出了错误“找不到源”并在调试器代码 Pane 中打开一个窗口,允许我进行导航。我已经在 xml 中创建了 2 个 View ,并且在保存时没有收到任何错误

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);
}
}

我想知道这个错误和之前的错误是否与代码无关,而是与我的设置有关。但是我已经创建了在版本 3 中运行的项目和 AVD。我查找了 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

关于android - 我正在尝试设置一个带有背景图片和 slider 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8398819/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com