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

只需在代码中将 Button 更改为 ImageButton 即可。

于 2013-07-02T07:14:25.667 回答
0

请在 XML 文件中将您的ImageButton 更改为 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 回答