我正在制作一个简单的应用程序,它使用数学技巧ti读取人的思想(技巧),但当我尝试运行它停止和崩溃,它永远不会从这里开始是我的主要活动
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}
答案 0 :(得分: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);
答案 1 :(得分:0)
请在XML文件中将 ImageButton更改为按钮 ..
或者
在Java类中将按钮更改为ImageButton
ImageButton btnn;
ImageButton btn;
btnn= (ImageButton) findViewById(R.id.Start);
btn= (ImageButton) findViewById(R.id.Exit);
答案 2 :(得分:0)
如果您错过了此步骤,请按以下步骤操作: 检查您的Android-Manifest.xml,如果您没有找到MainActivity的条目,请添加以下内容:
<activity
android:name=".MainActivity"
android:label="@string/any_string">
</activity>
如果适合你......
答案 3 :(得分:0)
您正在将ImageButton转换为Button类型。你做错了什么。 应该是ImageButton类型。
答案 4 :(得分:0)
只需在代码中将按钮更改为ImageButton。