我有这个fragment1,我想把来自另一个片段的java放在这里。问题是,我导入的java来自MainActivity.java,如果我复制它,它似乎不起作用。我已经为它导入了字符串和XML,只需要知道如何正确地将java放入这个:
当前片段
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ConnectFragment extends Fragment {
public ConnectFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_connect, container, false);
return rootView;
}
}
我要导入的Java:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public String sign = "";
public String total= "";
public Double tempDouble, tempDouble2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Buttons for Felony
Button button1 =(Button)findViewById(R.id.button1);
Button button2 =(Button)findViewById(R.id.button2);
Button button3 =(Button)findViewById(R.id.button3);
Button button4 =(Button)findViewById(R.id.button4);
Button button5 =(Button)findViewById(R.id.button5);
//Buttons for misdemeanors
Button button6 =(Button)findViewById(R.id.button7);
Button button7 =(Button)findViewById(R.id.button8);
Button button8 =(Button)findViewById(R.id.button6);
//Buttons for Infraction
Button button9 =(Button)findViewById(R.id.button9);
//clear, +, =
Button button10=(Button)findViewById(R.id.button10);
Button button11 =(Button)findViewById(R.id.button11);
Button button12 =(Button)findViewById(R.id.button12);
// button 1 event handler
button1.setOnClickListener(
//button 1 interface
new Button.OnClickListener()
{
//Button 1 callback method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("100 years");
}
}
);
// button 2 event handler
button2.setOnClickListener(
//button 2 interface
new Button.OnClickListener()
{
//Button 2 callback method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("25 years");
}
}
);
// button 3 event handler
button3.setOnClickListener(
//button 3 interface
new Button.OnClickListener()
{
//Button 3 callback method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("10 years");
}
}
);
// button 4 event handler
button4.setOnClickListener(
//button 4 interface
new Button.OnClickListener()
{
//Button 4 callback method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("5 years");
}
}
);
// button 5 event handler
button5.setOnClickListener(
//button 5 interface
new Button.OnClickListener()
{
//Button 5 callback method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("1 years");
}
}
);
// button 6 event handler
button6.setOnClickListener(
//button 1 interface
new Button.OnClickListener()
{
//Button 6 callback method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("1 years");
}
}
);
// button 7 event handler
button7.setOnClickListener(
//button 7 interface
new Button.OnClickListener()
{
//Button 7 callback method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("0.5 years");
}
}
);
// button 8 event handler
button8.setOnClickListener(
//button 8 interface
new Button.OnClickListener()
{
//Button 8 callback method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("0.25 years");
}
}
);
// button 9 event handler
button9.setOnClickListener(
//button 9 interface
new Button.OnClickListener()
{
//Button 9 callback method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.append("0.013 years");
}
}
);
// button 10 event handler
button10.setOnClickListener(
//button 10 interface
new Button.OnClickListener()
{
//Button 10 callback method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
output.setText("");
}
}
);
// button 12 event handler
button12.setOnClickListener(
//button 12 interface
new Button.OnClickListener()
{
//Button 12 callback method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
tempDouble = Double.parseDouble(output.getText().toString());
output.setText("");
sign = "+";
}
}
);
// button 11 event handler
button11.setOnClickListener(
//button 11 interface
new Button.OnClickListener()
{
//Button 11 callback method
public void onClick(View v)
{
TextView output = (TextView)findViewById(R.id.editText);
tempDouble2 = Double.parseDouble(output.getText().toString());
if (sign=="+")
{
output.setText(Double.toString(tempDouble+tempDouble2));
sign="";
}
}
}
);
}
}
答案 0 :(得分:1)
您的代码正在破坏,因为您需要执行rootView.findViewById(R.id.the_item_id)来获取对所有UI组件的引用。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_connect, container, false);
Button aButton = (Button) rootView.findViewById(R.id.button_id);
return rootView;
}
这会对你有所帮助