简体   繁体   English

链接按钮到下一个Android活动崩溃了应用程序

[英]Linking button to next android activity crashes app

I am trying to have my app go to activity_vehicle.xml when I click the button at the bottom of the activity_main.xml. 当我单击activity_main.xml底部的按钮时,我试图将我的应用程序转到activity_vehicle.xml。 When I click it the app crashes and I end up with a lot of junk in my log file. 当我点击它时,应用程序崩溃,我的日志文件中有很多垃圾。 I am running eclipse with the android sdk plugin. 我用android sdk插件运行eclipse。 As a side note when I try to type text into the text fields its in japanese, and the screen is acting like its turned long ways rather then standing straight up. 作为旁注当我尝试在文本字段中键入文本时,用日语表示,屏幕表现得像是转过头而不是直立。

Here is the button code taken from activity_main.xml where the button is located 以下是从按钮所在的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" />

This is my android manifest class where I declared the 2nd activity 这是我的android清单类,我宣布了第二个活动

<?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>

This is my MainActivity.java file where I set the onclick listener for the button. 这是我的MainActivity.java文件,我为该按钮设置了onclick监听器。 I assume the new start activity would use the Vechicle.java file I have and not the activity_vehicle.xml file 我假设新的启动活动将使用我拥有的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;
    }

}

}

When the project is ran the following errors pop up in LogCat I deleted the dates to make sure not many of them took more then 1 line 当项目运行时,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)

Change this 改变这个

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

to

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

You can use MainActivity.this in a annonymous inner class as in that case this is not a valid context. 您可以在匿名内部类中使用MainActivity.this,因为在这种情况下,这不是有效的上下文。

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

And your MainActivity extends Activtiy 您的MainActivity extends Activtiy

or 要么

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

this refers to Activity Context. this指的是活动背景。

Change this 改变这个

  <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>

to

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

It is better to change your package name coz if you reference google play services and have imports such as import com.google.android.gms.maps.CameraUpdate; 最好更改您的包名称,如果您引用谷歌播放服务并具有导入,如import com.google.android.gms.maps.CameraUpdate; . So its is better you change the package name to something more relevant. 因此,将包名更改为更相关的更好。 ` Use Explicit Intents. `使用显式意图。

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. 例如,响应用户操作启动新活动或启动服务以在后台下载文件。

Try this.. 尝试这个..

Change startActivity(new Intent("com.google.android.gms.Vehicle")); 更改startActivity(new Intent("com.google.android.gms.Vehicle")); to startActivity(new Intent(MainActivity.this,Vehicle.class)); to startActivity(new Intent(MainActivity.this,Vehicle.class));

private void destination_next_buttonClick()
{
    startActivity(new Intent(MainActivity.this,Vehicle.class));
}

Try to Change this line: 尝试更改此行:

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

to

Intent myIntent = new Intent(MainActivity.this, YourOtherActivity.class);
    startActivity(myIntent); 

Try to Change this 尝试改变这一点

<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>

to

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

It is better to change your package name. 最好更改您的包名称。 Hope it will helps you. 希望它能帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM