简体   繁体   English

Eclipse生成无效活动

[英]Eclipse Generating invalid Activity

I am currently working on an android project for school named MyContactList and at first when I generated new Activities it worked fine. 我目前正在为学校一个名为MyContactList的android项目工作,起初,当我生成新的Activity时,它运行良好。
However, now when I generate new Activities it pops out a ton of errors. 但是,现在当我生成新的活动时,它会弹出很多错误。
How do I go about generating a new activity: Right click com.example.mycontactlist under source > new > other > android activity > blank activity. 如何生成新活动:在源>新建>其他> android活动>空白活动下,右键单击com.example.mycontactlist。

It generates the files fine. 它生成的文件很好。 However, after generating, it shows these errors: 但是,生成后,将显示以下错误:

问题/错误

The code generated in the old Activities and in the new one is different. 旧活动和新活动中生成的代码不同。

Old: 旧:

package com.example.mycontactlist;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.google.android.gms.maps.GoogleMap.OnMyLocationChangeListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.common.ConnectionResult;

import android.app.Fragment;
import android.os.Bundle;

import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Point;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Build;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class ContactMapActivity extends FragmentActivity {

New: 新:

package com.example.mycontactlist;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class ContactSensorActivity extends ActionBarActivity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_sensor);
    }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.contact_sensor, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

I tried copying and pasting an old Activity and changing the code around to use as a new one. 我尝试复制和粘贴旧的Activity,然后更改代码以用作新的Activity。
I also tried creating one from scratch but each time it crashes and I am unable to find what the error is due to. 我也尝试过从头开始创建一个,但是每次崩溃时,我都找不到错误原因。

I have to test on my cellphone and currently do not know how to get it to notify me of crash errors. 我必须在手机上进行测试,目前不知道如何获取它以将崩溃错误通知我。

The reason I can't check it in the emulator is due to an old issue I had with google play services and my app: it didn't like my app and wouldn't update, so I run tests that have to do with the map portion of my code on my cellphone. 我无法在模拟器中检查它的原因是由于我在使用Google Play服务和我的应用程序时遇到了一个老问题:它不喜欢我的应用程序并且不会更新,所以我运行了与将我的代码的一部分映射到手机上。

My question is: how do I get it to generate an Activity like my old one? 我的问题是:如何获得像以前一样的活动?

Eclipse is generating different code than I expected when creating a new Activity. Eclipse生成的代码与我创建新的Activity时预期的代码不同。

You are creating different types of activities. 您正在创建不同类型的活动。 You can learn about different types of activities here: https://developer.android.com/studio/projects/templates.html I think you are creating a Navigation Drawer activity. 您可以在此处了解不同类型的活动: https : //developer.android.com/studio/projects/templates.html我认为您正在创建导航抽屉活动。

  1. Use Android Studio or convince your professor/class that it is the tool to use. 使用Android Studio或说服您的教授/班级使用该工具。 Eclipse has long been abandoned for Android development. 长期以来,Eclipse已被放弃用于Android开发。 The code is what you are learning anyway, not the tool to compile it. 无论如何, 代码都是您正在学习的东西,而不是编译它的工具。

  2. There is no way an empty / blank Activity template creates that "old" code. 空/空白的活动模板无法创建该“旧”代码。 That is a Google Map activity. 那是Google Map活动。

  3. android.app.support.v7 libraries are missing. 缺少android.app.support.v7库。 The majority of your compilation errors relate to that. 您的大部分编译错误都与此有关。 You are missing a JAR file (since you are using Eclipse) or a Gradle dependency (if you even use Gradle). 您缺少JAR文件(因为您正在使用Eclipse)或Gradle依赖项(如果您甚至使用Gradle)。

  4. activity_contact_sensor.xml have been generated by the IDE tools. activity_contact_sensor.xml由IDE工具生成。 It also should be able to resolve if the remainder of your code can compile. 它还应该能够确定您的其余代码是否可以编译。

tl;dr Not sure how that "old" code was generated. tl; dr不知道该“旧”代码是如何生成的。 Use Android Studio 使用Android Studio

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

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