无法启动活动ClassCastException:无法将activity1强制转换为activity2

时间:2013-09-09 08:35:18

标签: java android exception casting

您好我有3个活动,主要活动,包含菜单,我可以在哪里开始另一项活动。 但我只能开始第一项活动。当我想要开始第二个活动时,我的应用程序关闭。使用以下logcat。 我希望这足够了,因为10米的活动有点长。

logcat的:

09-08 18:50:41.185: D/AndroidRuntime(3065): Shutting down VM
09-08 18:50:41.195: W/dalvikvm(3065): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-08 18:50:41.285: E/AndroidRuntime(3065): FATAL EXCEPTION: main
09-08 18:50:41.285: E/AndroidRuntime(3065): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.vkedco.mobappdev.draw_touch_drive_00001/org.vkedco.mobappdev.draw_touch_drive_00001.e_10m}: java.lang.ClassCastException: org.vkedco.mobappdev.draw_touch_drive_00001.y_50m cannot be cast to org.vkedco.mobappdev.draw_touch_drive_00001.y_10m
09-08 18:50:41.285: E/AndroidRuntime(3065):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-08 18:50:41.285: E/AndroidRuntime(3065):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-08 18:50:41.285: E/AndroidRuntime(3065):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-08 18:50:41.285: E/AndroidRuntime(3065):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-08 18:50:41.285: E/AndroidRuntime(3065):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 18:50:41.285: E/AndroidRuntime(3065):     at android.os.Looper.loop(Looper.java:137)
09-08 18:50:41.285: E/AndroidRuntime(3065):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-08 18:50:41.285: E/AndroidRuntime(3065):     at java.lang.reflect.Method.invokeNative(Native Method)
09-08 18:50:41.285: E/AndroidRuntime(3065):     at java.lang.reflect.Method.invoke(Method.java:511)
09-08 18:50:41.285: E/AndroidRuntime(3065):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-08 18:50:41.285: E/AndroidRuntime(3065):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-08 18:50:41.285: E/AndroidRuntime(3065):     at dalvik.system.NativeStart.main(Native Method)
09-08 18:50:41.285: E/AndroidRuntime(3065): Caused by: java.lang.ClassCastException: org.vkedco.mobappdev.draw_touch_drive_00001.y_50m cannot be cast to org.vkedco.mobappdev.draw_touch_drive_00001.y_10m
09-08 18:50:41.285: E/AndroidRuntime(3065):     at org.vkedco.mobappdev.draw_touch_drive_00001.e_10m.onCreate(e_10m.java:107)
09-08 18:50:41.285: E/AndroidRuntime(3065):     at android.app.Activity.performCreate(Activity.java:5104)
09-08 18:50:41.285: E/AndroidRuntime(3065):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-08 18:50:41.285: E/AndroidRuntime(3065):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-08 18:50:41.285: E/AndroidRuntime(3065):     ... 11 more
09-08 18:50:44.225: I/Process(3065): Sending signal. PID: 3065 SIG: 9

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.vkedco.mobappdev.draw_touch_drive_00001"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="org.vkedco.mobappdev.draw_touch_drive_00001.MainActivity"
        android:label="@string/title_activity_draw_touch_drive_main"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="org.vkedco.mobappdev.draw_touch_drive_00001.e_50m"
        android:label="@string/title_activity_main"
        android:screenOrientation="portrait" >
    </activity>
    <activity
        android:name="org.vkedco.mobappdev.draw_touch_drive_00001.e_10m"
        android:label="@string/title_activity_rifle_10m" >
    </activity>
</application>

</manifest> 

MainActivity:

public class MainActivity extends Activity {    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnSimple = (Button) findViewById(R.id.e_50m);
        btnSimple.setOnClickListener(new OnClickListener() {
           public void onClick(View v) {
               Intent intent = new Intent(MainActivity.this, e_50m.class);
               startActivity(intent);             
           }
        });

        Button btnSimple2 = (Button) findViewById(R.id.e_10m);
        btnSimple2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, e_10m.class);
                startActivity(intent);
            }
        });
    }
}


public class e_10m extends Activity{


public RelativeLayout mContent;
y_10m mTicTacToeView = null;
public static String tempDir;
public File mypath;
 public static Bitmap mBitmap;
public static int width;
public static int height;
public static int r = 0;
public static float x;
public static float y;

public static float X;
public static float Y;


public static double vzdalenost;



 public String current = null;
 private String uniqueId;
 publicy_10m mSignature;
 public View mView;
 static TextView i;
 public static int q=0;






public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.zaznam_ran);      







   File directory = new File(Environment.getExternalStorageDirectory() + "/e_10m");
   if(!directory.exists())

       directory.mkdir();

   uniqueId =  "_" ;
   current = uniqueId + ".png";


   mypath= new File(directory,current);

   mContent = (RelativeLayout) findViewById(R.id.rl);

   mSignature = new y_10m(this, null);
   mSignature.setBackgroundColor(Color.TRANSPARENT);
   mContent.addView(mSignature, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

   mView = mContent;







    Display display = getWindowManager().getDefaultDisplay();
     width = (display.getWidth());
     height = (display.getHeight());        
    mTicTacToeView = (y_10m) this.findViewById(R.id.pntr);







Button btn3 = (Button) findViewById(R.id.button3);

btn3.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {


        if(y_10m.t == 10){
            y_10m.Vycistit();

        }
        y_10m.PridatRanu();

    }
});





Button btn4 = (Button) findViewById(R.id.button4);

btn4.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        y_10m.krouzkyMazat();


        if (q>=1){q = q-1;}


    }
});


 Button btn5 = (Button) findViewById(R.id.button5);

    btn5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Krouzky_10m.Vycistit();

            q=0;
            y_10m.t = 0;

        }
    });




     Button btn6 = (Button) findViewById(R.id.button6);

        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    Log.v("log_tag", "Panel Saved");

                        mView.setDrawingCacheEnabled(true);


                }

        });



}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();

    getMenuInflater().inflate(R.menu.activity_draw_touch_drive_main, menu);
    return true;
}

@Override
protected void onDestroy() {

    super.onDestroy();      
    if ( mTicTacToeView != null ) {
        mTicTacToeView.NemuzemeVykreslovat();
        mTicTacToeView = null;

    }
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    if ( mTicTacToeView != null ) {
        mTicTacToeView.NemuzemeVykreslovat();
        mTicTacToeView = null;
    }
}


@Override
protected void onPause() {
    super.onPause();
    if ( mTicTacToeView != null ) {
        mTicTacToeView.NemuzemeVykreslovat();
    }
}



@Override
protected void onStart() {
    super.onStart();
    if ( mTicTacToeView != null ) {
        mTicTacToeView.MuzemeVykreslovat();
    }
    else {
        mTicTacToeView = (y_10m) this.findViewById(R.id.pntr);
        mTicTacToeView.MuzemeVykreslovat();
    }

    y_10m.Vycistit();

        q=0;
        y_10m.t = 0;


}

@Override
protected void onStop() {
    super.onStop();
    if ( mTicTacToeView != null ) {
        mTicTacToeView.NemuzemeVykreslovat();
    }

}


 } 

zaznam_ran中的pntr视图:

<org.vkedco.mobappdev.draw_touch_drive_00001.y_50m
    android:id="@+id/pntr"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:tag="Painter" />

1 个答案:

答案 0 :(得分:0)

问题在于,在您的代码中,您将y_50m View投射到y_10m View

下面

mTicTacToeView = (y_10m) this.findViewById(R.id.pntr);

将其替换为

mTicTacToeView = (y_50m) this.findViewById(R.id.pntr);