应用程序显示错误" myapp已停止"

时间:2014-04-10 15:28:20

标签: java android eclipse

这是我第一次创建Android应用程序,这种情况发生了。 任何帮助表示赞赏。

更改清单后,这是我更新的logcat

04-10 16:00:17.154: E/AndroidRuntime(2480): FATAL EXCEPTION: main
04-10 16:00:17.154: E/AndroidRuntime(2480): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.miraapp/com.example.miraapp.MainActivity}: java.lang.ClassCastException: com.example.miraapp.MainActivity cannot be cast to android.view.View$OnClickListener
04-10 16:00:17.154: E/AndroidRuntime(2480):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-10 16:00:17.154: E/AndroidRuntime(2480):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-10 16:00:17.154: E/AndroidRuntime(2480):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-10 16:00:17.154: E/AndroidRuntime(2480):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-10 16:00:17.154: E/AndroidRuntime(2480):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 16:00:17.154: E/AndroidRuntime(2480):     at android.os.Looper.loop(Looper.java:137)
04-10 16:00:17.154: E/AndroidRuntime(2480):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-10 16:00:17.154: E/AndroidRuntime(2480):     at java.lang.reflect.Method.invokeNative(Native Method)
04-10 16:00:17.154: E/AndroidRuntime(2480):     at java.lang.reflect.Method.invoke(Method.java:511)
04-10 16:00:17.154: E/AndroidRuntime(2480):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-10 16:00:17.154: E/AndroidRuntime(2480):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-10 16:00:17.154: E/AndroidRuntime(2480):     at dalvik.system.NativeStart.main(Native Method)
04-10 16:00:17.154: E/AndroidRuntime(2480): Caused by: java.lang.ClassCastException: com.example.miraapp.MainActivity cannot be cast to android.view.View$OnClickListener
04-10 16:00:17.154: E/AndroidRuntime(2480):     at com.example.miraapp.MainActivity.onCreate(MainActivity.java:30)
04-10 16:00:17.154: E/AndroidRuntime(2480):     at android.app.Activity.performCreate(Activity.java:5104)
04-10 16:00:17.154: E/AndroidRuntime(2480):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-10 16:00:17.154: E/AndroidRuntime(2480):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-10 16:00:17.154: E/AndroidRuntime(2480):     ... 11 more

这是我的代码,我有另一个新活动

package com.example.miraapp;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity 
{


Button button1;
EditText etResponse;
TextView tvIsConnected;


@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener((OnClickListener) this);


}

private void button1Click()
{
    startActivity(new Intent("com.example.miraapp.GUI")); 
}
public void onClick(View v)
{

    switch (v.getId())
    {
        case R.id.button1:
            button1Click();
            break;
        case R.id.button2:
            button2Click();
            break;
    }
}

private void button2Click()
{


}

};

顺便说一句,这是我的activity_main

  <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:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 android:background="#000000"
 tools:context="com.example.miraapp.MainActivity$PlaceholderFragment" >

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="131dp"
    android:text="@string/START_fix" 
    android:onClick="button1Click"/>

 <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="29dp"
    android:contentDescription="@string/title_fix"
    android:src="@drawable/mira4" />

  <Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="63dp"
    android:text="@string/CONNECT_fix" />

  </RelativeLayout>

这是我更新的androidmanifest.xml

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.example.miraapp"
 android:versionCode="1"
 android:versionName="1.0"
 >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17"

 />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.miraapp.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.example.miraapp.GUI"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.miraapp.GUI" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

 </application>

</manifest>

3 个答案:

答案 0 :(得分:1)

试试这个..

从应用程序中的mainfest中删除 android:name="Mira",如下所示

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

还有一件事......

startActivity(new Intent("com.example.miraapp.GUI"));方法

中将startActivity(new Intent(MainActivity.this,GUI.class));更改为button1Click(),如下所示
private void button1Click()
{
    startActivity(new Intent(MainActivity.this,GUI.class)); 
}

修改

public class MainActivity extends Activity implements OnClickListener

然后

button1.setOnClickListener((OnClickListener) this);更改为

button1.setOnClickListener(this); 

答案 1 :(得分:1)

应用

  

需要维护全局应用程序状态的基类。   您可以通过在您的名称中指定其名称来提供自己的实现   AndroidManifest.xml的应用程序标记,它将导致该类   在你的过程中为你实例化   应用程序/包已创建。

所以你有

<application
android:name="Mira

并且堆栈跟踪说

  

引起:java.lang.ClassNotFoundException:没找到类   &#34; com.example.miraapp.Mira&#34;在路径上:   /data/app/com.example.miraapp-2.apk

你也说

  

我只有MainActivity类

从应用程序标记

中删除android:name="Mira

您还需要使用显式意图

  

显式意图指定组件以名称开头(   完全合格的班级名称)。 您通常会使用明确的意图   在您自己的应用中启动组件,因为您知道类名   您想要开始的活动或服务。例如,开始一个新的   响应用户操作或启动服务下载的活动   文件在后台

SO使用

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

由于你有明确的意图,你可以摆脱intent-filter

 <intent-filter>
        <action android:name="com.example.miraapp.GUI" />
        <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>

编辑:

  

引起:java.lang.ClassCastException:   com.example.miraapp.MainActivity无法转换为   android.view.View $ OnClickListener

更改此

public class MainActivity extends Activity 

public class MainActivity extends Activity implements OnClickListener

然后改变这个

 button1.setOnClickListener((OnClickListener) this);
 // this refers to the Activity. but you cast it to (OnClickListener) this

button1.setOnClickListener(this); 

您需要您的类来实现接口OnClickListener

所以你需要button1.setOnClickListener(this);

答案 2 :(得分:-1)

MainActivity.java

package com.example.miraapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    Button button1;
    EditText etResponse;
    TextView tvIsConnected;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);

    }

    private void button1Click() {
        startActivity(new Intent(this, GUI.class));
    }

    public void onClick(View v) {

        switch (v.getId()) {
        case R.id.button1:
            button1Click();
            break;
        case R.id.button2:
            button2Click();
            break;
        }
    }

    private void button2Click() {

    }

};

manifest.java

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.miraapp.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.example.miraapp.GUI"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

GUI.java

package com.example.miraapp;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class GUI extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Toast.makeText(getApplicationContext(), "hello", 1000).show();
    }

}

activity_main.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: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=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="94dp"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="44dp"
        android:text="Button" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="46dp"
        android:layout_marginRight="34dp"
        android:ems="10" >

        <requestFocus />
    </EditText>

</RelativeLayout>