我改变了一些我认为无关的事情,运行程序,并遇到了这个错误:
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): FATAL EXCEPTION: main
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.prattia.webs.cheaterphysics/com.prattia.webs.cheaterphysics.Vectors}: java.lang.ClassCastException: android.widget.TextView
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): at android.os.Looper.loop(Looper.java:123)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): at java.lang.reflect.Method.invoke(Method.java:521)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): at dalvik.system.NativeStart.main(Native Method)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): Caused by: java.lang.ClassCastException: android.widget.TextView
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): at com.prattia.webs.cheaterphysics.Vectors.onCreate(Vectors.java:52)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-05 14:53:33.286: ERROR/AndroidRuntime(29084): ... 11 more
我在网上搜索并发现最可能的情况是两个小部件具有相同的ID。我没有在我的XML中找到它。这是XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical">
<ImageView android:layout_height="wrap_content"
android:src="@drawable/vectors"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:adjustViewBounds="true">
</ImageView>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<Button android:text="Choose Program"
android:id="@+id/bChsProgV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="97dp"
android:adjustViewBounds="true">
</Button>
<ImageButton android:layout_height="wrap_content"
android:src="@drawable/help"
android:id="@+id/ibHelpV"
android:layout_width="wrap_content"
android:layout_marginLeft="65dp"
android:background="@null"
android:layout_marginTop="10dp">
</ImageButton>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:text="Value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="#fffafa">
</TextView>
<EditText android:text=""
android:id="@+id/etValueV"
android:layout_height="wrap_content"
android:inputType="numberSigned|numberDecimal"
android:layout_width="100dp" >
</EditText>
<Button android:text="Next"
android:id="@+id/bNextV"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp">
</Button>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:text="Angle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="#fffafa">
</TextView>
<EditText android:text=""
android:id="@+id/etAngleV"
android:layout_height="wrap_content"
android:inputType="numberSigned|numberDecimal"
android:layout_width="100dp" >
</EditText>
<Button android:text="Prev"
android:id="@+id/bPrevV"
android:layout_width="65dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp">
</Button>
</LinearLayout>
<TextView android:text=""
android:id="@+id/tvAnsValV"
android:textColor="#fffafa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="left"
android:paddingLeft="5dp">
</TextView>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<LinearLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:checked="true"
android:text="Degrees"
android:layout_marginLeft="5dp"
android:id="@+id/rbDegV">
</RadioButton>
<RadioButton android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:checked="false"
android:text="Radians"
android:layout_marginLeft="5dp"
android:id="@+id/rbRadV">
</RadioButton>
</RadioGroup>
</LinearLayout>
<RadioButton android:text="π"
android:id="@+id/rbPiV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:visibility="invisible"
android:layout_marginTop="50dp">
</RadioButton>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<LinearLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
<RadioButton android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:checked="false"
android:text="Q2"
android:layout_marginLeft="30dp"
android:id="@+id/rbQ2V">
</RadioButton>
<RadioButton android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:checked="false"
android:text="Q3"
android:layout_marginLeft="30dp"
android:id="@+id/rbQ3V">
</RadioButton>
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical">
<RadioButton android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:checked="true"
android:text="Q1"
android:layout_marginLeft="5dp"
android:id="@+id/rbQ1V">
</RadioButton>
<RadioButton android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:checked="false"
android:text="Q4"
android:layout_marginLeft="5dp"
android:id="@+id/rbQ4V">
</RadioButton>
</LinearLayout>
</LinearLayout>
</RadioGroup>
</LinearLayout>
<Button android:text="Submit"
android:id="@+id/bSubmitV"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_gravity="center">
</Button>
<TextView android:text=""
android:id="@+id/tvErrorV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#fffafa">
</TextView>
<LinearLayout android:id="@+id/llCanvasV"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</LinearLayout>
</LinearLayout>
</ScrollView>
这是代码的一部分,标有第52行。如果您需要更多代码我会提出来。不幸的是,这很长。 VectorsView尚未完成,可能无法正常工作。它甚至不是我正在研究的计划的一部分。任何帮助都会很棒。
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.vectors);
vectorsView = new VectorsView(this);
l = (LinearLayout) findViewById(R.id.llCanvasV); //Line 52
l.addView(vectorsView);
...}
public class VectorsView extends View{
public VectorsView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawLine(0, 0, 100, 100, paint);
vectorsView.draw(canvas);
vectorsView.setLayoutParams(new LayoutParams(25, 25));
}
}
public void Initialize(){
chsprog = (Button) findViewById(R.id.bChsProgV);
next = (Button) findViewById(R.id.bNextV);
prev = (Button) findViewById(R.id.bPrevV);
submit = (Button) findViewById(R.id.bSubmitV);
value = (EditText) findViewById(R.id.etValueV);
angle = (EditText) findViewById(R.id.etAngleV);
help = (ImageButton) findViewById(R.id.ibHelpV);
deg = (RadioButton) findViewById(R.id.rbDegV);
rad = (RadioButton) findViewById(R.id.rbRadV);
q1 = (RadioButton) findViewById(R.id.rbQ1V);
q2 = (RadioButton) findViewById(R.id.rbQ2V);
q3 = (RadioButton) findViewById(R.id.rbQ3V);
q4 = (RadioButton) findViewById(R.id.rbQ4V);
pi = (RadioButton) findViewById(R.id.rbPiV);
error = (TextView) findViewById(R.id.tvErrorV);
ansval = (TextView) findViewById(R.id.tvAnsValV);
}
答案 0 :(得分:2)
引起:java.lang.ClassCastException:android.widget.TextView
您正在尝试将TextView
投射到LinearLayout
。但是您的XML似乎是正确的,因此请查看清理整个项目是否有帮助,有时 Eclipse / ADT 会混乱,并且不会正确重建资源,导致ID指向错误的地方。