我想用one button
和one slider
编写一个简单的Android应用程序:我的java program
如下:
package com.example.android.apis.view;
import com.example.android.apis.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;
/**
* Uses a TextSwitcher.
*/
public class TextSwitcher1 extends Activity implements ViewSwitcher.ViewFactory,
View.OnClickListener, OnSeekBarChangeListener {
SeekBar mSeekBar;
TextView mProgressText;
TextView mTrackingText;
private TextSwitcher mSwitcher;
private int mCounter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_switcher_1);
setContentView(R.layout.seekbar_1);
mSeekBar = (SeekBar)findViewById(R.id.seek);
mSeekBar.setOnSeekBarChangeListener(this);
mProgressText = (TextView)findViewById(R.id.progress);
mTrackingText = (TextView)findViewById(R.id.tracking);
mSwitcher = (TextSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
Button nextButton = (Button) findViewById(R.id.next);
nextButton.setOnClickListener(this);
updateCounter();
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
mProgressText.setText(progress + " " +
getString(R.string.seekbar_from_touch) + "=" + fromTouch);
}
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));
}
public void onClick(View v) {
mCounter++;
updateCounter();
}
private void updateCounter() {
mSwitcher.setText(String.valueOf(mCounter));
}
public View makeView() {
TextView t = new TextView(this);
t.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
t.setTextSize(36);
return t;
}
}
我收到了这个错误:
The method setOnSeekBarChangeListener(SeekBar.OnSeekBarChangeListener) in the type SeekBar is not applicable for the arguments (TextSwitcher1) in this line : mSeekbar.setonSeekbarChangeListener(this);
我不知道这是什么问题,请你帮我解决一下 非常感谢,Stefan
答案 0 :(得分:0)
问题是你使用setContentView两次,所以第一个布局将被第二个布局替换,任何对第一个布局的引用都将为null。
setContentView(R.layout.text_switcher_1);
setContentView(R.layout.seekbar_1);
要在主版面中使用其他资源视图,您应该将该布局扩展到视图,然后将其初始化为子视图,如下面的代码:
setContentView(R.layout.text_switcher_1);
LayoutInflater inflater = (LayoutInflater)this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
View layoutSeekBarView = inflater.inflate(R.layout.seekbar_1, null);
mSeekBar = (SeekBar)layoutSeekBarView.findViewById(R.id.seek);
mSeekBar.setOnSeekBarChangeListener(this);
mProgressText = (TextView)layoutSeekBarView.findViewById(R.id.progress);
mTrackingText = (TextView)layoutSeekBarView.findViewById(R.id.tracking);
mSwitcher = (TextSwitcher)this.findViewById(R.id.switcher);
mSwitcher.setFactory(this);
Button nextButton = (Button) findViewById(R.id.next);
nextButton.setOnClickListener(this);