我一直在尝试从一个通过接口扩展MediaRecorder的类更新我的MainActivity中的TextView但它不起作用。我知道findViewById方法只适用于布局已经膨胀的Activity,但为什么Button OnClickListener接口完美地使用findViewById方法?所以我想知道是否有办法可以做到这一点。
public class mediaa extends MediaRecorder {
Context c;
public mediaa(Context c) {
this.c = c;
}
interface AmplitudeListener {
void update(int i, TextView tv);
}
AmplitudeListener al;
public void setAmplitudeListener(AmplitudeListener al) {
this.al = al;
}
void updateAmplitude() {
View inflatedView = View.inflate(c, R.layout.main, null);
TextView tv = (TextView) inflatedView.findViewById(R.id.tv);
al.update(getMaxAmplitude(), tv);
}
@Override
public void start() throws IllegalStateException {
super.start();
handler.postDelayed(run, 500);
}
final Handler handler = new Handler();
Runnable run = new Runnable() {
@Override
public void run() {
updateAmplitude();
handler.postDelayed(this, 500);
}
};
@Override
public void stop() throws IllegalStateException {
super.stop();
}
@Override
public int getMaxAmplitude() throws IllegalStateException {
return super.getMaxAmplitude();
}
public class MainActivity extends Activity implements OnClickListener, AmplitudeListener {
mediaa m;
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
m().start();
break;
case R.id.stop:
m().stop();
}
}
mediaa m() {
m = new mediaa(this);
m.setAudioSource(mediaa.AudioSource.MIC);
m.setOutputFormat(mediaa.OutputFormat.AAC_ADTS);
m.setAudioEncoder(mediaa.AudioEncoder.AAC);
m.setAudioEncodingBitRate(320000);
m.setAudioSamplingRate(44100);
m.setAmplitudeListener(this);
m.setOutputFile("/sdcard/Testt.m4a");
try {
m.prepare();
} catch (IOException e) {} catch (IllegalStateException e) {}
return m;
}
@Override
public void update(int i, TextView tv) {
try {
View inflatedView = new
MainActivity().getLayoutInflater().inflate(R.layout.main, null);
tv = (TextView) inflatedView.findViewById(R.id.tv);
tv.setText(i);
} catch (Exception e) {
Toast.makeText(this, "" + e, Toast.LENGTH_LONG).show();
}
}
public TextView getView(int i) {
TextView tv = (TextView) findViewById(i);
return tv;
}
Button start, stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
start.setOnClickListener(this);
stop.setOnClickListener(this);
}
}
答案 0 :(得分:0)
调用findviewById只能获取某组类的引用(例如View,ViewHolder等),你需要其中一个才能调用findViewById。
您也可以像往常一样在活动中获得参考,并将其传递给您需要的其他课程。这是一个例子:
[1] "/home/masi/Workspace"
Warning message:
In sink() : no sink to remove
在构建构造函数时,您还可以获取此TextView的引用,因此您将其作为参数传递。这是确保避免异常的好方法(例如,您忘记了setView())
public class MainActivity extends AppCompatActivity {
private TextView myTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView = (TextView) findViewById(R.id.<EXAMPLE_ID>);
MyExampleClass class = new MyExampleClass();
class.setView(myTextView);
class.<EXAMPLE_ACTION>();
}
//// ExampleClass
public class MyExampleClass{
TextView tv;
//Empty Constructor Example
MyExampleClass(){
}
public void setView(TextView tv){
this.tv = tv;
}
public void exampleMethod(){
tv.setText("asd");
}
}
将来请使用代码示例括号。很难通读你的代码。