java - 将按钮链接到下一个 Android Activity 会导致应用程序崩溃

标签 java android eclipse android-intent

当我单击 Activity_main.xml 底部的按钮时,我试图让我的应用程序转到 Activity_vehicle.xml。当我点击它时,应用程序崩溃了,我的日志文件中最终出现了大量垃圾。我正在使用 android sdk 插件运行 eclipse。作为旁注,当我尝试在文本字段中输入日语文本时,屏幕的表现就像是旋转了很长一段路,而不是直立着。

这是从按钮所在的activity_main.xml中获取的按钮代码

<Button
    android:id="@+id/destination_next_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="22dp"
    android:layout_marginRight="37dp"
    android:text="Next" />

这是我的 Android list 类,我在其中声明了第二个 Activity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.gms"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.google.android.gms.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.google.android.gms.Vehicle"
        android:label="@string/title_activity_vehicle" >
                        <action android:name="com.google.android.gms.Vehicle" />

            <category android:name="android.intent.category.DEFAULT" />
    </activity>
</application>

这是我的 MainActivity.java 文件,我在其中设置按钮的 onclick 监听器。我假设新的启动 Activity 将使用我拥有的 Vechicle.java 文件,而不是 Activity_vehicle.xml 文件

public class MainActivity extends Activity implements OnClickListener 
{

Button destination_next_button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    destination_next_button = (Button)findViewById(R.id.destination_next_button);
    destination_next_button.setOnClickListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private void destination_next_buttonClick()
{
    startActivity(new Intent("com.google.android.gms.Vehicle"));
}

@Override
public void onClick(View v) {
    switch (v.getId())
    {
    case R.id.destination_next_button:
        destination_next_buttonClick();
        break;
    }

}

}

当项目运行时,LogCat 中会弹出以下错误,我删除了日期以确保其中没有太多错误超过 1 行

D/AndroidRuntime(428): Shutting down VM
W/dalvikvm(428): threadid=1: thread exiting with uncaught exception (group=0x40014760)
E/AndroidRuntime(428): FATAL EXCEPTION: main
E/AndroidRuntime(428): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.google.android.gms.Vehicle }

E/AndroidRuntime(428):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)

E/AndroidRuntime(428):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1382)

E/AndroidRuntime(428):  at android.app.Activity.startActivityForResult(Activity.java:3044)
E/AndroidRuntime(428):  at android.app.Activity.startActivity(Activity.java:3150)
E/AndroidRuntime(428):  at com.google.android.gms.MainActivity.destination_next_buttonClick(MainActivity.java:34)

E/AndroidRuntime(428):  at bsu.edu.cs222.gascalculator.MainActivity.onClick(MainActivity.java:42)

E/AndroidRuntime(428):  at android.view.View.performClick(View.java:3100)
E/AndroidRuntime(428):  at android.view.View$PerformClick.run(View.java:11644)
E/AndroidRuntime(428):  at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(428):  at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(428):  at android.os.Looper.loop(Looper.java:126)
E/AndroidRuntime(428):  at android.app.ActivityThread.main(ActivityThread.java:3997)
E/AndroidRuntime(428):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(428):  at java.lang.reflect.Method.invoke(Method.java:491)
E/AndroidRuntime(428):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)

E/AndroidRuntime(428):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
E/AndroidRuntime(428):  at dalvik.system.NativeStart.main(Native Method)

最佳答案

改变这个

  startActivity(new Intent("com.google.android.gms.Vehicle"));

  startActivity(new Intent(MainActivity.this,Vehicle.class));

您可以在匿名内部类中使用 MainActivity.this,因为在这种情况下,这不是有效的上下文。

java.lang.Object
   ↳    android.content.Context // see this
       ↳    android.content.ContextWrapper
           ↳    android.view.ContextThemeWrapper
               ↳    android.app.Activity //see this

并且您的MainActivity 扩展了Activtiy

  startActivity(new Intent(this,Vehicle.class));

this 指的是 Activity 上下文。

改变这个

  <activity
    android:name="com.google.android.gms.Vehicle"
    android:label="@string/title_activity_vehicle" >
    <action android:name="com.google.android.gms.Vehicle" />
    <category android:name="android.intent.category.DEFAULT" />
 </activity>

  <activity
    android:name="com.google.android.gms.Vehicle"
    android:label="@string/title_activity_vehicle" >
  </activity> 

最好更改您的包名称,因为如果您引用 google play 服务并具有诸如 import com.google.android.gms.maps.CameraUpdate; 等导入内容。因此,最好将包名称更改为更相关的名称。 ` 使用显式 Intent 。

Explicit intents specify the component to start by name (the fully-qualified class name). You'll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. For example, start a new activity in response to a user action or start a service to download a file in the background.

关于java - 将按钮链接到下一个 Android Activity 会导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22777249/

相关文章:

java - 反转字符串,不起作用

android - ionic 错误: may not have the required environment or OS to build this project

mysql - Eclipse/Netbeans 能否在 PostgreSQL 中运行 "inline"PHP/Perl

android - 如何在android/eclipse上设置Jfeinstein10滑动菜单

java - 集合可以包含重复项吗?

Java 类型删除和重载?

android - onCreateContextMenu 仅在 API 15 和 16 上抛出错误

android - 在 android 布局中的微调器旁边添加和图像

c++ - 如何在 Eclipse 和 Linux 上介入代码实现 new()?

java - 当手机没有任何互联网时无法从 url 获取图标,或者是否可以保存在内部存储然后从那里读取