搜索栏和按钮不起作用

问题描述:

我想编写一个简单的Android应用程序与one buttonone slider:我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); 

我不知道这个问题是什么,请你帮我解决。
非常感谢您提前,斯蒂芬

+0

这似乎是正确的,清洁的项目可能是有帮助 – 2014-09-06 09:44:32

+0

谢谢Arash。是的,我已经清理了它,但它不起作用。我想附上我的整个代码,但我不知道我可以将我的代码附加到此页面上吗?请将您的电子邮件发送给我,以便将您的整个代码发送给我?谢谢您好。斯特凡 – Stefan 2014-09-06 12:37:06

+0

对不起,我的个人资料中无法看到您的电子邮件地址,因为您的个人资料是私密的,因为您无法在我的个人资料中看到我的电子邮件地址。请您在此给我写下您的电子邮件地址。非常感谢。 Stefan – Stefan 2014-09-06 13:37:33

问题是你使用setContentView两次,所以第一个布局将被秒取代,任何引用到您的第一个布局将为空。

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); 
+0

感谢Arash.I应用了这些更改,但它仍然无法正常工作。 – Stefan 2014-09-07 08:54:22