为什么按钮会强制关闭应用程序,没有突出显示的错误,因为我是 android 开发的新手,所以我找不到问题。感谢任何帮助。
这是xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="447dp"
android:background="@drawable/background"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Categories" >
<Button
android:id="@+id/author"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:background="@drawable/buttonpress"
android:onClick="Author"
android:text="@string/Author"
android:textColor="#FA6900"
android:textSize="21sp" />
<Button
android:id="@+id/religion"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="22dp"
android:background="@drawable/buttonpress"
android:onClick="Religion"
android:text="@string/Religion"
android:textColor="#FA6900"
android:textSize="21sp" />
<Button
android:id="@+id/politician"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="22dp"
android:background="@drawable/buttonpress"
android:onClick="Politician"
android:text="@string/Politician"
android:textColor="#FA6900"
android:textSize="21sp" />
<Button
android:id="@+id/happy"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="23dp"
android:background="@drawable/buttonpress"
android:onClick="Happiness"
android:text="@string/happy"
android:textColor="#FA6900"
android:textSize="21sp" />
<Button
android:id="@+id/sport"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="22dp"
android:background="@drawable/buttonpress"
android:onClick="Sport"
android:text="@string/Sport"
android:textColor="#FA6900"
android:textSize="21sp" />
</LinearLayout>
</ScrollView>
爪哇
package com.android.motivateme3;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.NavUtils;
public class Categories extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categories);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {@link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
public void Sport(View v)
{
Intent intent = new Intent(getApplicationContext(), Sport.class);
Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(25);
startActivity(intent);
}
public void Religion(View v)
{
Intent intent = new Intent(getApplicationContext(), Religion.class);
Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(25);
startActivity(intent);}
public void Author(View v)
{
Intent intent = new Intent(getApplicationContext(), Author.class);
Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(25);
startActivity(intent);}
public void Politician(View v)
{
Intent intent = new Intent(getApplicationContext(), Politician.class);
Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(25);
startActivity(intent);}
public void Happiness(View v)
{
Intent intent = new Intent(getApplicationContext(), Happiness.class);
Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(25);
startActivity(intent);}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.categories, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
日志猫
05-04 16:08:41.014: D/memalloc(28579): /dev/pmem: Mapped buffer base:0x60519000 size:18690048 offset:14921728 fd:73
05-04 16:08:41.054: D/memalloc(28579): /dev/pmem: Mapped buffer base:0x61a6c000 size:35233792 offset:31465472 fd:79
05-04 16:08:41.064: D/OpenGLRenderer(28579): Flushing caches (mode 0)
05-04 16:08:41.074: D/memalloc(28579): /dev/pmem: Unmapping buffer base:0x5c63f000 size:31465472 offset:27697152
05-04 16:08:41.074: D/memalloc(28579): /dev/pmem: Unmapping buffer base:0x5ebfb000 size:10366976 offset:6598656
05-04 16:08:41.084: D/memalloc(28579): /dev/pmem: Unmapping buffer base:0x5f5de000 size:14921728 offset:11153408
05-04 16:08:41.824: D/memalloc(28579): /dev/pmem: Mapped buffer base:0x5c63f000 size:14921728 offset:11153408 fd:64
05-04 16:08:42.344: D/AndroidRuntime(28579): Shutting down VM
05-04 16:08:42.344: W/dalvikvm(28579): threadid=1: thread exiting with uncaught exception (group=0x40ac4210)
05-04 16:08:42.354: E/AndroidRuntime(28579): FATAL EXCEPTION: main
05-04 16:08:42.354: E/AndroidRuntime(28579): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.motivateme3/com.android.motivateme3.Author}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ImageButton
05-04 16:08:42.354: E/AndroidRuntime(28579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
05-04 16:08:42.354: E/AndroidRuntime(28579): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
05-04 16:08:42.354: E/AndroidRuntime(28579): at android.app.ActivityThread.access$600(ActivityThread.java:127)
05-04 16:08:42.354: E/AndroidRuntime(28579): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
05-04 16:08:42.354: E/AndroidRuntime(28579): at android.os.Handler.dispatchMessage(Handler.java:99)
05-04 16:08:42.354: E/AndroidRuntime(28579): at android.os.Looper.loop(Looper.java:137)
05-04 16:08:42.354: E/AndroidRuntime(28579): at android.app.ActivityThread.main(ActivityThread.java:4441)
05-04 16:08:42.354: E/AndroidRuntime(28579): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 16:08:42.354: E/AndroidRuntime(28579): at java.lang.reflect.Method.invoke(Method.java:511)
05-04 16:08:42.354: E/AndroidRuntime(28579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
05-04 16:08:42.354: E/AndroidRuntime(28579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
05-04 16:08:42.354: E/AndroidRuntime(28579): at dalvik.system.NativeStart.main(Native Method)
05-04 16:08:42.354: E/AndroidRuntime(28579): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.ImageButton
05-04 16:08:42.354: E/AndroidRuntime(28579): at com.android.motivateme3.Author.setupActionBar(Author.java:39)
05-04 16:08:42.354: E/AndroidRuntime(28579): at com.android.motivateme3.Author.onCreate(Author.java:28)
05-04 16:08:42.354: E/AndroidRuntime(28579): at android.app.Activity.performCreate(Activity.java:4465)
05-04 16:08:42.354: E/AndroidRuntime(28579): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-04 16:08:42.354: E/AndroidRuntime(28579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
05-04 16:08:42.354: E/AndroidRuntime(28579): ... 11 more
05-04 16:08:49.964: I/Process(28579): Sending signal. PID: 28579 SIG: 9
05-04 16:11:35.094: D/memalloc(28746): /dev/pmem: Mapped buffer base:0x5f932000 size:10366976 offset:6598656 fd:70
显现
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="15" />
<uses-feature android:name="@string/hello_world" />
<uses-feature android:name="@string/Politician" />
<uses-feature android:name="@string/Sport" />
<uses-feature android:name="@string/Religion" />
<uses-feature android:name="@string/Author" />
<uses-feature android:name="@string/happy" />
<uses-feature android:name="@string/responseno" />
<uses-feature android:name="@string/responseyes" />
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@drawable/logo"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.android.motivateme3.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.android.motivateme3.Sport"
android:label="@string/title_activity_sport"
android:parentActivityName="com.android.motivateme3.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.android.motivateme3.Categories" />
</activity>
<activity
android:name="com.android.motivateme3.Religion"
android:label="@string/title_activity_religion"
android:parentActivityName="com.android.motivateme3.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.android.motivateme3.Categories" />
</activity>
<activity
android:name="com.android.motivateme3.Author"
android:label="@string/title_activity_author"
android:parentActivityName="com.android.motivateme3.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.android.motivateme3.Categories" />
</activity>
<activity
android:name="com.android.motivateme3.Politician"
android:label="@string/title_activity_politician"
android:parentActivityName="com.android.motivateme3.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.android.motivateme3.Categories" />
</activity>
<activity
android:name="com.android.motivateme3.Sports"
android:label="@string/title_activity_sports"
android:parentActivityName="com.android.motivateme3.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.android.motivateme3.Categories" />
</activity>
<activity
android:name="com.android.motivateme3.Happiness"
android:label="@string/title_activity_happiness"
android:parentActivityName="com.android.motivateme3.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.android.motivateme3.Categories" />
</activity>
<activity
android:name="com.android.motivateme3.Feedback"
android:label="@string/title_activity_feedback"
android:parentActivityName="com.android.motivateme3.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.android.motivateme3.MainActivity" />
</activity>
<activity
android:name="com.android.motivateme3.SettingsActivity"
android:label="@string/title_activity_settings"
android:parentActivityName="com.android.motivateme3.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.android.motivateme3.MainActivity" />
</activity>
<activity
android:name="com.android.motivateme3.Categories"
android:label="@string/title_activity_categories"
android:parentActivityName="com.android.motivateme3.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.android.motivateme3.MainActivity" />
</activity>
<activity
android:name="com.android.motivateme3.Favourites"
android:label="@string/title_activity_favourites" >
</activity>
</application>
</manifest>
作者活动java包com.android.motivateme3;
import java.util.Random;
import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
public class Author extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_author);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
ImageButton NextQuote = (ImageButton)findViewById(R.id.next);
final TextView display = (TextView) findViewById(R.id.textView1);
NextQuote.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(25);
Random numGen = new Random();
int rNumber = numGen.nextInt(10);
if (rNumber == 0)
{
display.setText(R.string.Author1);
}
else if (rNumber == 1)
{
display.setText(R.string.Author2);
}
else if (rNumber == 2)
{
display.setText(R.string.Author3);
}
else if (rNumber == 3)
{
display.setText(R.string.Author4);
}
else if (rNumber == 4)
{
display.setText(R.string.Author5);
}
else if (rNumber == 5)
{
display.setText(R.string.Author6);
}
else if (rNumber == 6)
{
display.setText(R.string.Author7);
}
else if (rNumber == 7)
{
display.setText(R.string.Author8);
}
else if (rNumber == 8)
{
display.setText(R.string.Author9);
}
else if (rNumber == 9)
{
display.setText(R.string.Author10);
} }
});
}
ImageButton Sharing = (ImageButton)findViewById(R.id.share);
Sharing.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(25);
TextView text = (TextView)findViewById(R.id.textView1);
String quote = text.getText().toString();{
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("plain/text");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "This is a great quote (from the Motivate Me! app)");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, quote);
startActivity(Intent.createChooser(shareIntent, "Share via:"));}};});}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.author, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML 作者
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Author" >
<ImageButton
android:id="@+id/next"
android:layout_width="140dp"
android:layout_height="50dp"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="14dp"
android:background="@drawable/buttonpress"
android:contentDescription="@string/Next"
android:onClick="NextQuote"
android:src="@drawable/navigationnextitem" />
<ImageButton
android:id="@+id/share"
android:layout_width="140dp"
android:layout_height="50dp"
android:layout_alignLeft="@+id/textView1"
android:layout_alignTop="@+id/next"
android:background="@drawable/buttonpress"
android:contentDescription="@string/share"
android:onClick="Sharing"
android:src="@drawable/socialshare" />
<TextView
android:id="@+id/textView1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:background="@drawable/button_shape"
android:text="@string/Getstarted"
android:textColor="#FF6900"
android:textSize="23sp" />
</RelativeLayout>