0
public class MainActivity extends AppCompatActivity implements
TopSectionFragment.TopSectionListener {
當我嘗試實施TopSectionFragment
書寫雲紅和,然後它說Cannot resolve symbol
當我的鼠標越過它。實現TopSectionFragment無法解析符號機器人工作室
這一切都發生在Android Studio中。
我MainActivity.java看起來像這樣:
package com.example.danielhunter.fragments;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;`
public class MainActivity extends AppCompatActivity
implements TopSectionFragment.TopSectionListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
我TopSectionFragment.java如下鏈接纔可這樣:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.ViewGroup;
import android.view.View;
import android.view.LayoutInflater;
import android.support.v4.app.Fragment;
import android.widget.Button;
import android.widget.EditText;
import android.app.Activity;
import com.example.danielhunter.fragments.R;
public class TopSectionFragment extends Fragment {
private static EditText topTextInput;
private static EditText bottomTextInput;
TopSectionListener activityCommander;
public interface TopSectionListener{
public void createMeme(String top,String bottom);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
activityCommander = (TopSectionListener) activity;
}catch (ClassCastException e){
throw new ClassCastException(activity.toString());
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.top_section_fragment, container, false);
topTextInput = (EditText) view.findViewById(R.id.topTextInput);
bottomTextInput = (EditText) view.findViewById(R.id.bottomTextInput);
final Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v){
buttonClicked(v);
}
}
);
return view;
}
// calls this when the button is clicked
public void buttonClicked(View view){
activityCommander.createMeme(topTextInput.getText().toString(), bottomTextInput.getText().toString());
}
}
MainActivity將如何顯示? –
如果您遵循Android Studio中的quickfix建議,它實際上會爲您創建方法。它看起來像這個'public void createMeme(String top,String bottom){}',或者本質上,就像接口定義一樣,但是有一個方法體。 –