简而言之,问题是我可以通过 id 找到我的元素,但它一直要求我将其转换为视图,而我无法将其转换为我的对象名称的类型。我不确定投射到视图是否应该是这样,但 Android 示例只是创建片段并添加它,因此它没有在 xml 中定义。但关键是,在示例中,对象不是视图类型。
更多信息:这可能是一个快速而简单的修复。我有一个片段类,其中包含我用代码创建的自定义视图(它是一种游戏,其中不断绘制)。我在 xml 布局的左上角添加了片段。片段下方是按钮(在 xml 布局中定义)。我有一个主要活动,我想要一个包含我的自定义视图的片段实例的句柄,这样当单击按钮时,片段中的一些变量会发生变化并重绘视图。
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment android:name="com.example.dcubebluetooth"
android:id="@+id/lEDView1"
android:layout_width="400px"
android:layout_height="400px" />
<Button
android:id="@+id/layer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lEDView1"
android:layout_marginTop="50px"
android:text="Layer1"
android:textSize="12dp" />
<Button
android:id="@+id/layer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/layer1"
android:layout_toRightOf="@id/layer1"
android:text="Layer2"
android:textSize="12dp" />
<Button
android:id="@+id/layer3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/layer2"
android:layout_toRightOf="@id/layer2"
android:text="Layer3"
android:textSize="12dp" />
<Button
android:id="@+id/layer4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/layer3"
android:layout_toRightOf="@id/layer3"
android:text="Layer4"
android:textSize="12dp" />
</RelativeLayout>
主要活动:
public class MainActivity extends Activity implements LEDGridFragment.TriggerBlueTooth{
Button l1;
Button l2;
Button l3;
Button l4;
BluetoothService bt;
LEDGridFragment gridUI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
@Override
public void onledSelected(int layer, int led) {
// TODO Auto-generated method stub
String msg = Integer.toString(layer);
if(led<10) msg+=0;
msg+= Integer.toString(led);
bt.write(msg);
}
//Full method below not showing. I don't know why
public void initialize(){
gridUI = findViewById(R.id.lEDView1);
bt = new BluetoothService();
l1 = (Button) findViewById(R.id.layer1);
l2 = (Button) findViewById(R.id.layer2);
l3 = (Button) findViewById(R.id.layer3);
l4 = (Button) findViewById(R.id.layer4);
l1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
gridUI.currentLayer = 0;
gridUI.update();
}
});
l2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
gridUI.currentLayer = 1;
gridUI.update();
}
});
l3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
gridUI.currentLayer = 2;
gridUI.update();
}
});
l4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
gridUI.currentLayer = 3;
gridUI.update();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
分段:
public class LEDGridFragment extends Fragment{
LEDView view;
TriggerBlueTooth mCallBack;
boolean[][][] states = new boolean[4][4][4];
int currentLayer = 0;
public interface TriggerBlueTooth{
public void onledSelected(int layer, int led);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mCallBack = (TriggerBlueTooth) activity;
view.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int x = (int) (event.getX())/100;
int y = (int) (event.getY())/100;
int led = y+(x*4);
mCallBack.onledSelected(currentLayer, led); //Notify Main Activity of the led that was toggled
//So it can send data over Bluetooth
return false;
}
});
}
public void update(){
view.drawGrid(states, currentLayer); //Update the view
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
for(int i=0; i<states.length; i++){ //Initialize the board off which the view will be drawn
for(int j=0; j<states[0].length; j++){
for(int k=0; k<states[0][0].length; k++){
states[i][j][k] = false;
}
}
}
view = new LEDView(getActivity());
return view;
//return super.onCreateView(inflater, container, savedInstanceState); //Auto-formed. Commented out
}
}
另一方面(好吧,如果你不回答这个问题):图形布局没有显示游戏初始绘图方面的假设。如果我添加视图本身,它会。但是现在,它是一个灰色框,带有“从'Fragment Layout'上下文菜单中选择预览布局”这个菜单在哪里?我想看看我的观点是否有效。我无法亲自测试它,因为 1. 它无法编译,以及 2. 我没有安卓手机