[英]Eclipse: 'ClassCastException' and GUI is not showing in Graphical Layout
I have created a custom component. 我创建了一个自定义组件。 Here is the code
这是代码
html_5_spinner.xml html_5_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="@+id/spinText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/upBtn"
android:layout_width="25dp"
android:layout_height="15dp"
android:background="@android:drawable/arrow_up_float" />
<Button
android:id="@+id/downBtn"
android:layout_width="25dp"
android:layout_height="15dp"
android:background="@android:drawable/arrow_down_float" />
</LinearLayout>
</LinearLayout>
HTML5Spinner.java HTML5Spinner.java
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class HTML5Spinner extends LinearLayout {
private EditText spinText;
private Button upBtn, downBtn;
public HTML5Spinner(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onFinishInflate()
{
super.onFinishInflate();
((Activity)getContext()).getLayoutInflater().inflate(R.layout.html5_spinner, this);
setupViewItems();
}
private void setupViewItems()
{
spinText = (EditText)findViewById(R.id.spinText);
upBtn = (Button)findViewById(R.id.upBtn);
downBtn = (Button)findViewById(R.id.downBtn);
//Register Listeners
upBtn.setOnClickListener(new UpButtonAction());
}
private class UpButtonAction implements OnClickListener
{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("workn", "It is running");
}
}
}
Main activity is using the above cusom component. 主要活动是使用上述cusom组件。 It just has a XML, no business logic or GUI rendering Java code
它只有XML,没有业务逻辑或呈现Java代码的GUI
activity_main.xml activity_main.xml
<ScrollView
android:id="@+id/scroller"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:drawablePadding="5dp"
android:text="RadioButton" />
<com.nFlate.nflate.HTML5Spinner
android:id="@+id/aasaas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></com.nFlate.nflate.HTML5Spinner>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:drawablePadding="5dp"
android:text="RadioButton" />
<com.nFlate.nflate.HTML5Spinner
android:id="@+id/aasaas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></com.nFlate.nflate.HTML5Spinner>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
</ScrollView>
</RelativeLayout>
This gives no error when running. 运行时不会出现任何错误。 But eclipse shows errors in "Graphical Layout" of
activity_main.xml
但是eclipse在
activity_main.xml
“图形布局”中显示错误
Below is the error 下面是错误
Exception raised during rendering: com.android.layoutlib.bridge.android.BridgeContext cannot be cast to android.app.Activity
Exception details are logged in Window > Show View > Error Log
java.lang.ClassCastException: com.android.layoutlib.bridge.android.BridgeContext cannot be cast to android.app.Activity
at com.xxx.xxx.HTML5Spinner.onFinishInflate(HTML5Spinner.java:27)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:754)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:749)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:749)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:749)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:749)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:372)
Below is the place where this error is displayed. 下面是显示此错误的地方。
Because of this error, the GUI is not getting displayed in eclipse Graphical Layout . 由于该错误,GUI不会以Eclipse 图形布局显示。 Why is this?
为什么是这样?
In your onFinishInflate
there's this problematic cast: 在您的
onFinishInflate
存在以下问题:
((Activity)getContext())
Eclipse Graphical Layout renders views with a Context
that is not an Activity
. Eclipse图形布局使用不是
Activity
的Context
渲染视图。 You shouldn't need this cast here anyway. 无论如何,您都不需要在这里进行演员表转换。
You are missing the library which renders the spinner. 您缺少渲染微调器的库。 Include that library in your project.
在您的项目中包括该库。 Please import library into the workspace in order to work properly and mark this library in the android option of the project properties.
请将库导入工作区,以使其正常工作,并在项目属性的android选项中将该库标记。 The main problem for this is not including the library properly.
这样做的主要问题是没有正确包含库。
如果在清单文件中将HTML5Spinner.java注册为活动,则它必须扩展“活动”类型才能继续进行,但是此处使用的LinearLayout不会启动,因此xml GUI也为ClassCastException给出了错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.