感谢是否有人可以提供帮助。
我的目标是将变量从活动传递到片段。 我试图在textview中显示所选单选按钮的文本(在活动中)(在同一活动中显示的片段中)。 我在行“BlankFragment bf =(BlankFragment)getFragmentManager()。findFragmentById(R.id.blankfragment);”中看到了上述错误。
我的mainactivity.java文件如下:
g = 0
temp = len(koord)
print(temp)
while g != temp:
(value1,x1,y1,a) = koord[g]
for i in range(g,temp - 1):
(value2,x2,y2,b) = koord[i+1]
if y1 >= y2 + b or y2 >= y1 + a:
diff[abs(value1 - value2)].append([value1,value2])
if x1 >= x2 + b or x2 >= x1 + a:
diff[abs(value1 - value2)].append([value1,value2])
g += 1
我的fragment.java文件如下:
package hk.ust.cse.comp107x.fragmentspractice;
import android.annotation.SuppressLint;
import android.support.v4.app.Fragment;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
/*RadioButton rb = (RadioButton) findViewById(checkedId);
String string = rb.getText().toString();
Log.d("demo", string);*/
BlankFragment bf = (BlankFragment) getFragmentManager().findFragmentById(R.id.blankfragment);
if (checkedId == R.id.radioButton) {
bf.changecolor(Color.RED);
} else if (checkedId == R.id.radioButton2) {
bf.changecolor(Color.BLUE);
} else if (checkedId == R.id.radioButton3) {
bf.changecolor(Color.GREEN);
} else if (checkedId == R.id.radioButton4) {
bf.changecolor(Color.YELLOW);
}
}
});
}
}
我的main_activity.xml如下:
package hk.ust.cse.comp107x.fragmentspractice;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
public void changecolor(int Color){
getActivity().findViewById(R.id.relative).setBackgroundColor(Color);
}
}
我的fragment.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="hk.ust.cse.comp107x.fragmentspractice.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/fragment"
android:layout_alignParentEnd="true"
android:layout_marginBottom="31dp"
android:layout_marginEnd="75dp"
android:text="@string/displaytextview" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGroup">
<RadioButton
android:id="@+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/redvalue" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/bluevalue" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/greenvalue" />
<RadioButton
android:id="@+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/yellowvalue" />
</RadioGroup>
<fragment
android:id="@+id/blankfragment"
android:name="hk.ust.cse.comp107x.fragmentspractice.BlankFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/radioGroup"
android:layout_centerHorizontal="true"
tools:layout="@layout/fragment_blank"/>
</RelativeLayout>