![](/img/trans.png)
[英]unfortunately, <your app> has stopped while i am trying it run it in my android emulator. how to solve this?
[英]The APK won't run on the android emulator. (Unfortunately the app has stopped)
我是android應用程序開發的新手。 我為一個簡單的應用程序編寫了一個android代碼,單擊“添加一個”按鈕,將應該在按鈕上方的文本字段中的總數加1,然后單擊“減去一個”按鈕,從上方的“文本”字段中減去1。 這是它的代碼。
package com.example.ocrtransbybee;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.*;
public class StartingPoint extends Activity {
int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (Button) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter--;
display.setText("Your total is " + counter);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.starting_point, menu);
return true;
}
}
布局xml代碼為:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".StartingPoint" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hey"
android:textSize="50sp"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/tvDisplay"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Button1"
android:textSize="15sp"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/bAdd"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Button2"
android:textSize="15sp"
android:layout_gravity="center"
android:gravity="center"
android:id="@+id/bSub"
/>
</LinearLayout>
以及運行時信息:
[2014-02-16 20:57:49 - OCRTransByBee] New emulator found: emulator-5554
[2014-02-16 20:57:49 - OCRTransByBee] Waiting for HOME ('android.process.acore') to be launched...
[2014-02-16 21:00:41 - OCRTransByBee] HOME is up on device 'emulator-5554'
[2014-02-16 21:00:41 - OCRTransByBee] Uploading OCRTransByBee.apk onto device 'emulator-5554'
[2014-02-16 21:00:42 - OCRTransByBee] Installing OCRTransByBee.apk...
[2014-02-16 21:02:04 - OCRTransByBee] Success!
[2014-02-16 21:02:05 - OCRTransByBee] Starting activity com.example.ocrtransbybee.StartingPoint on device emulator-5554
[2014-02-16 21:02:11 - OCRTransByBee] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.ocrtransbybee/.StartingPoint }
它不是在模擬器中啟動的,代碼對我來說似乎還不錯,在我朋友的筆記本電腦上的學校里也能正常運行,每次我嘗試運行它時,都會說“不幸的是,該應用程序已停止”。
您正在使用Button
投射TextView
。
TextView display;
你有
display = (Button) findViewById(R.id.tvDisplay);
應該
display = (TextView) findViewById(R.id.tvDisplay);
您嘗試使用Button類強制轉換display(TextView)對象。
只需更換
display = (Button) findViewById(R.id.tvDisplay);
同
display = (TextView) findViewById(R.id.tvDisplay);
代碼中存在強制轉換類異常。
問題出在此行代碼中。
display = (Button) findViewById(R.id.tvDisplay);
改變是
display= (TextView)findViewById(R.id.tvDisplay);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.