更改活动AppCompatActivity导致程序故障 - 的Eclipse(Android版) [英] Change activity to AppCompatActivity leads to program failure- Eclipse(Android)

查看:253
本文介绍了更改活动AppCompatActivity导致程序故障 - 的Eclipse(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我4个月开始使用Eclipse和Android。虽然我创建具有默认设置的新项目 - 在manifest文件中找到空activity.I,它扩展了活动。不过,我想通过更改活动AppCompatActivity有一个动作条。虽然目前并没有直接显示一个错误,当我开始它模拟器程序将立即停止。

我有一个默认设置为另一个项目:用动作条空项目。于是我试着为什么通过简单地改变活动AppCompatActivity将无法正常工作,找出。

我觉得有什么不对的支持库,所以我加入了Android的支持-V7-应用程序兼容性作为一个引用的库。但是,程序仍然不能正常工作。

 包com.example.tracker;进口android.content.Intent;
进口android.os.Bundle;
进口android.support.v7.app.AppCompatActivity;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.Button;公共类MainActivity扩展AppCompatActivity {    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        //获取按钮
        按钮按钮1 =(按钮)findViewById(R.id.button1);
        button1.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                意向意图=新意图(MainActivity.this,SecondActivity.class);
                intent.setAction(Intent.ACTION_VIEW);
                startActivity(意向);
            }
        });
    }
}

logcat的:
10月7日至27日:06:44.858:E / AndroidRuntime(597):致命异常:主要
10月7日至27日:06:44.858:E / AndroidRuntime(597):了java.lang.RuntimeException:无法实例活动ComponentInfo {com.example.tracker / com.example.tracker.MainActivity}:抛出java.lang.ClassNotFoundException:COM .example.tracker.MainActivity
10月7日至27日:06:44.858:E / AndroidRuntime(597):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
10月7日至27日:06:44.858:E / AndroidRuntime(597):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10月7日至27日:06:44.858:E / AndroidRuntime(597):在android.app.ActivityThread.access $ 600(ActivityThread.java:123)
10月7日至27日:06:44.858:E / AndroidRuntime(597):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)
10月7日至27日:06:44.858:E / AndroidRuntime(597):在android.os.Handler.dispatchMessage(Handler.java:99)
10月7日至27日:06:44.858:E / AndroidRuntime(597):在android.os.Looper.loop(Looper.java:137)
10月7日至27日:06:44.858:E / AndroidRuntime(597):在android.app.ActivityThread.main(ActivityThread.java:4424)
10月7日至27日:06:44.858:E / AndroidRuntime(597):在java.lang.reflect.Method.invokeNative(本机方法)
10月7日至27日:06:44.858:E / AndroidRuntime(597):在java.lang.reflect.Method.invoke(Method.java:511)
10月7日至27日:06:44.858:E / AndroidRuntime(597):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)

 <?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.tracker
    安卓版code =1
    机器人:=的versionName1.0>    <用途-SDK
        安卓的minSdkVersion =14
        机器人:targetSdkVersion =21/>    <应用
        机器人:allowBackup =真
        机器人:图标=@绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名字=。MainActivity
            机器人:标签=@字符串/ APP_NAME>
            &所述;意图滤光器>
                <作用机器人:名字=android.intent.action.MAIN/>                <类机器人:名字=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:名字=。SecondActivity
            机器人:标签=@字符串/秒>
        < /活性GT;
    < /用途>< /清单>

下面的含量研究是菜单:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    工具:上下文=com.example.tracker.MainActivity
    >    <项目
        机器人:ID =@ + ID / myitem
        机器人:orderInCategory =2
        机器人:标题=myitem
        应用:showAsAction =从不/>
    <项目
        机器人:ID =@ + ID / action_settings
        机器人:orderInCategory =1
        机器人:标题=action_settings
        应用:showAsAction =从不/>&所述; /菜单>


解决方案

您需要将您的活动添加到清单。

 <活动机器人:名字=com.example.tracker.MainActivity/>

如果它已经存在,尝试移植到Android的工作室,因为它可能是Eclipse的相关问题,并处理相关性的方式。

I am four month new to Eclipse and Android. While I am creating a new project with default setting - empty activity.I found in the Manifest file, it extends activity. However, I want to have an actionbar by changing the activity to AppCompatActivity. Although there does not show an error directly, the program will stop immediately after I start it on emulator.

I have another project with default setting as: empty project with actionbar. So I was trying to find out why by simply changing the activity to AppCompatActivity will not work.

I think there is something wrong with the support library, so I added the android-support-v7-appcompat as a Referenced Libraries. However, the program still does not work.

package com.example.tracker;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

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

        //get the button
        Button button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent= new Intent(MainActivity.this,SecondActivity.class);
                intent.setAction(Intent.ACTION_VIEW);
                startActivity(intent);
            }
        });
    }
}

Logcat: 07-27 10:06:44.858: E/AndroidRuntime(597): FATAL EXCEPTION: main 07-27 10:06:44.858: E/AndroidRuntime(597): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tracker/com.example.tracker.MainActivity}: java.lang.ClassNotFoundException: com.example.tracker.MainActivity 07-27 10:06:44.858: E/AndroidRuntime(597): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880) 07-27 10:06:44.858: E/AndroidRuntime(597): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 07-27 10:06:44.858: E/AndroidRuntime(597): at android.app.ActivityThread.access$600(ActivityThread.java:123) 07-27 10:06:44.858: E/AndroidRuntime(597): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 07-27 10:06:44.858: E/AndroidRuntime(597): at android.os.Handler.dispatchMessage(Handler.java:99) 07-27 10:06:44.858: E/AndroidRuntime(597): at android.os.Looper.loop(Looper.java:137) 07-27 10:06:44.858: E/AndroidRuntime(597): at android.app.ActivityThread.main(ActivityThread.java:4424) 07-27 10:06:44.858: E/AndroidRuntime(597): at java.lang.reflect.Method.invokeNative(Native Method) 07-27 10:06:44.858: E/AndroidRuntime(597): at java.lang.reflect.Method.invoke(Method.java:511) 07-27 10:06:44.858: E/AndroidRuntime(597): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

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

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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=".SecondActivity"
            android:label="@string/second" >
        </activity>
    </application>

</manifest>

The contens below are menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.tracker.MainActivity" 
    >

    <item
        android:id="@+id/myitem"
        android:orderInCategory="2"
        android:title="myitem"
        app:showAsAction="never"/>
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="1"
        android:title="action_settings"
        app:showAsAction="never"/>

</menu>

解决方案

You need to add your activity to the Manifest.

<activity android:name="com.example.tracker.MainActivity"/>

If it's already there, try migrating to Android Studio because it might be an issue related to Eclipse, and the way it handles dependencies.

这篇关于更改活动AppCompatActivity导致程序故障 - 的Eclipse(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆