0

私は数学のトリックを使用して人々の心を読む単純なアプリを作成しています(トリック)が、実行しようとすると停止してクラッシュし、ここで開始されないことが私の主な活動です

public class MainActivity extends Activity {
 Button btnn;
 Button btn; 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(android.R.style.Theme_DeviceDefault);
    setContentView(R.layout.activity_main);
     btnn= (Button) findViewById(R.id.Start);
     btn= (Button) findViewById(R.id.Exit);
     btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setMessage("Are you sure you want to exit?");
        //  builder.setCancelable(false);
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    MainActivity.this.finish();
                }
            });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });
    btnn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent toNextPage = new Intent(MainActivity.this,
                    two.class); 
            startActivity(toNextPage);
            MainActivity.this.finish();

        }
    }); 
}}

ここに私のxmlファイルがあります

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to Brain Reader"
        android:textAppearance="?android:attr/textAppearanceLarge"
          android:textStyle="bold"
          android:layout_marginLeft="16dp"
    android:typeface="serif" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium" 
          android:textStyle="bold"
          android:textSize="22dp"
    android:typeface="serif"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageButton
            android:id="@+id/Start"
            android:layout_width="110dp"
            android:layout_height="50dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/ic_launche"
            android:layout_marginLeft="10dp" />

        <ImageButton
            android:id="@+id/Exit"
            android:layout_width="110dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/ex"
            android:layout_marginRight="10dp" />

    </RelativeLayout>

</LinearLayout>

誰でも助けてください事前に感謝します私のログの猫は言います

07-02 12:05:00.927: D/ThemeManager(18835): packageName=====com.example.brainreader
07-02 12:05:00.931: D/ThemeManager(18835): packageName=com.example.brainreader
07-02 12:05:01.043: E/AndroidRuntime(18835): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.brainreader/com.example.brainreader.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
07-02 12:05:01.043: E/AndroidRuntime(18835):    at com.example.brainreader.MainActivity.onCreate(MainActivity.java:21)
07-02 12:05:01.047: W/ActivityManager(231):   Force finishing activity com.example.brainreader/.MainActivity
07-02 12:05:01.163: D/AES(231):     process : com.example.brainreader
07-02 12:05:01.163: D/AES(231):      module : com.example.brainreader v1 (1.0)
07-02 12:05:01.165: D/AEE/LIBAEE(231): shell: raise_exp(2, 18835, -1361051648, com.example.brainreader, 0x0x213dd00, 0x0x0)
07-02 12:05:01.165: E/AEE/LIBAEE(231): read_cmdline:com.example.brainreader
07-02 12:05:01.577: W/ActivityManager(231): Activity pause timeout for ActivityRecord{41f62250 com.example.brainreader/.MainActivity}
07-02 12:05:01.577: V/ActivityManager(231): Enqueueing pending finish: ActivityRecord{41f62250 com.example.brainreader/.MainActivity}
07-02 12:05:01.689: I/InputDispatcher(231): channel '41480db8 Starting com.example.brainreader (server)' ~ abortBrokenDispatchCycle - notify=false
07-02 12:05:07.434: I/ActivityManager(231): Process com.example.brainreader (pid 18835) has died.
07-02 12:05:07.434: V/ActivityManager(231): Dying app: ProcessRecord{41c57800 18835:com.example.brainreader/10095}, pid: 18835, thread: android.os.BinderProxy@418da480
07-02 12:05:07.440: V/ActivityManager(231): Removing app ProcessRecord{41c57800 0:com.example.brainreader/10095} from list [ActivityRecord{4210c030 com.android.contacts/.DialtactsContactsEntryActivity}, ActivityRecord{41455818 com.android.phone/.InCallScreen}, ActivityRecord{41691d88 com.gionee.launcher/.GNLauncherActivity}] with 3 entries
07-02 12:05:07.440: V/ActivityManager(231): Removing app ProcessRecord{41c57800 0:com.example.brainreader/10095} from list [] with 0 entries
07-02 12:05:07.440: V/ActivityManager(231): Removing app ProcessRecord{41c57800 0:com.example.brainreader/10095} from list [] with 0 entries
07-02 12:05:07.440: V/ActivityManager(231): Removing app ProcessRecord{41c57800 0:com.example.brainreader/10095} from list [] with 0 entries
07-02 12:05:07.440: V/ActivityManager(231): Removing app ProcessRecord{41c57800 0:com.example.brainreader/10095} from list [] with 0 entries
07-02 12:05:11.654: W/ActivityManager(231): Activity destroy timeout for ActivityRecord{41f62250 com.example.brainreader/.MainActivity}
4

5 に答える 5

1

ボタンを次の代わりに ImageButton に変更します。

btnn= (Button) findViewById(R.id.Start);
btn= (Button) findViewById(R.id.Exit);

使用する :

btnn= (ImageButton) findViewById(R.id.Start);
btn= (ImageButton) findViewById(R.id.Exit);
于 2013-07-02T07:08:50.647 に答える
0

このステップが欠落している場合は、Android-Manifest.xml を確認してください。MainActivity のエントリが見つからない場合は、次を追加します。

<activity
            android:name=".MainActivity"
            android:label="@string/any_string">
</activity>

それがあなたのために働くなら.....

于 2013-07-02T07:11:55.350 に答える
0

ImageButton を Button タイプにキャストしています。あなたがしていることは間違っています。ImageButton タイプである必要があります。

于 2013-07-02T07:13:04.307 に答える
0

ImageButton をXML ファイルのButtonに変更してください。

または

Java クラスでButton を ImageButtonに変更します

ImageButton btnn;
ImageButton btn; 

btnn= (ImageButton) findViewById(R.id.Start);
btn= (ImageButton) findViewById(R.id.Exit);
于 2013-07-02T07:09:01.837 に答える
0

コードで Button を ImageButton に変更するだけです。

于 2013-07-02T07:14:25.667 に答える