Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何链接到其他类_Java_Android - Fatal编程技术网

Java 如何链接到其他类

Java 如何链接到其他类,java,android,Java,Android,我在一个类中有很多方法,这些元素引用1个XML布局文件。如何将这些方法放入额外的类中 因此,我想以这段代码为例: public void recordSound(){ // abgerunderter Rahmen für den Medienplayer wird in einer TextView // angezeigt TextView soundUmrandung = (TextView) findViewById(R.id.soundumrandungte

我在一个类中有很多方法,这些元素引用1个XML布局文件。如何将这些方法放入额外的类中

因此,我想以这段代码为例:

public void recordSound(){  
    // abgerunderter Rahmen für den Medienplayer wird in einer TextView
    // angezeigt
    TextView soundUmrandung = (TextView) findViewById(R.id.soundumrandungtextview);

    // das Design (abgerundeter Rahmen) für den Medienplayer laden
    Resources res = getApplicationContext().getResources();
    Drawable drawable = res.getDrawable(R.drawable.soundshape);

    // das geladene Design auf der TextView anwenden, damit ein abgerunter
    // Rahmen erscheint
    soundUmrandung.setBackground(drawable);


    ImageButton record = (ImageButton) findViewById(R.id.sound);
    ImageButton recordStop = (ImageButton) findViewById(R.id.soundstop);

哪些元素将同一布局文件引用到不同的类中

我试过了

LayoutInflater layoutinflater = (LayoutInflater) this
            .getSystemService(LAYOUT_INFLATER_SERVICE);
    final View openprojectView = layoutinflater.inflate(R.layout.openproject, null);
要设置LayoutResource并使用intent调用新类(如
intent soundRecord=new intent(this,soundRecord.class);startActivity(soundRecord);
,但这会导致错误:

07-20 21:19:00.590: E/AndroidRuntime(20296): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.unitnode/com.unitnode.SoundRecord}: android.view.InflateException: Binary XML file line #88: Error inflating class fragment
现在,我在新课上尝试了这种方法:

@凌驾 创建时受保护的void(Bundle savedInstanceState){

并参考布局:
TextView soundUmrandung=(TextView)openprojectView.findViewById(R.id.soundumrandungtextview);
。结果不再是错误,但它显示了一个新的空白视图。当我关闭新的空白视图时,我看到了所有内容,但按钮丢失了clickListener。因此我无法再录制音频

当我试图用
新声音记录(上下文)
打开新类时,按钮也丢失了ClickListener。当我从Activity扩展类时,这似乎对我来说是错误的,因为该类不应打开新Activity。该类应该只能访问父Activity的元素

所以我的班级现在:

公开课录音{

在OpenProject中,该类的调用方式为:
newsoundrecord(context);


请帮帮我。我真的需要把我的代码分成几个类。

你的java代码与
R.layout.openproject
的关系如何?你是否尝试过在膨胀后将活动发送到这两个类?在java代码中,我需要访问R.layout.openproject的元素(例如箭头左键).对不起,你发送这两门课的活动到底是什么意思?
07-20 21:19:00.590: E/AndroidRuntime(20296): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.unitnode/com.unitnode.SoundRecord}: android.view.InflateException: Binary XML file line #88: Error inflating class fragment
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.openproject);
    LayoutInflater layoutinflater = (LayoutInflater) this
            .getSystemService(LAYOUT_INFLATER_SERVICE);
    openprojectView = layoutinflater.inflate(R.layout.openproject, null);
    //TextView sound = (TextView) openprojectView.findViewById(R.id.soundumrandungtextview);
    //Log.d("openprojectView", "openprojectView " + openprojectView + "sound " + sound);
    recordSound();

}
private SharedPreferences sharedPreferences;
private MediaRecorder mediaRecorder = null;
private File soundFile = null;
private String soundFilePath;
private View openprojectView;
//final Context context = this;

public SoundRecord(Context context){

    LayoutInflater layoutinflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    //setContentView(R.layout.openproject);
    //LayoutInflater layoutinflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    openprojectView = layoutinflater.inflate(R.layout.openproject, null);
    //TextView sound = (TextView) openprojectView.findViewById(R.id.soundumrandungtextview);
    //Log.d("openprojectView", "openprojectView " + openprojectView + "sound " + sound);
    recordSound(context);

}