Android Studio Basic的第一个练习

时间:2015-11-30 23:18:58

标签: java xml android-studio

我一直在使用Android Studio创建我的第一个应用。我没有使用另一个IDE,我认为这个练习在以下几个方面都出现了错误。

  • 我的活动
  • 显示消息活动
  • 摇篮

DisplayMessageActivity

`package com.company.myfirstapp;


import android.annotation.TargetApi;
import android.os.Build;

import static android.os.Build.*;

/**
* Created by MyActivity on 17/10/2015.
*/

public class DisplayMessageActivity extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Get the message from the intent
            Intent intent = getIntent();
            String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);

            // Create the text view
            TextView textView = new TextView(this);
            textView.setTextSize(40);
            textView.setText(message);

            // Set the text view as the activity layout
            setContentView(textView);
    }

    @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();
            return super.onOptionsItemSelected(item);
    }

}`

MyActivity

    package com.company.myfirstapp;

     import android.content.Intent;
     import android.support.v7.app.ActionBarActivity;
     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.EditText;

     import com.company.myfirstapp.DisplayMessageActivity;
     import com.company.myfirstapp.R;

     public class MyActivity extends AppCompatActivity {
      final public static String EXTRA_MESSAGE =                                                                                                                                                                                                                                                                                                            "com.company.myfirstapp.MESSAGE";

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_my, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        MyActivity.this.startActivity(intent);
        }
    }
}

apply plugin: 'com.android.application'

final def extension = android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
    applicationId "com.company.myfirstapp"
    minSdkVersion 8
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'),     'proguard-rules.pro'
    }
 }
}
final def extension1 = extension
extension1

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
}

我的第一个应用程序

// Top-level build file where you can add configuration options common to                 `all sub-projects/modules.

    buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
     }
    }

    allprojects {
    repositories {
        jcenter()
    }
  }

我在“我的活动”中的“显示消息活动”和“显示消息”中的“覆盖”和“公共无效”中存在错误。

1 个答案:

答案 0 :(得分:0)

首先从(MyActivity.java)修复:

   final public static String EXTRA_MESSAGE = "Some message";  // fix this line

您的gradle脚本已全部搞砸了,请更改为此应用的build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.company.myfirstapp"
        minSdkVersion 8
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
         release {
             minifyEnabled true
             proguardFiles getDefaultProguardFile('proguard-android.txt'),     'proguard-rules.pro'
         }
 }

 dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile 'com.android.support:appcompat-v7:23.1.1'
 }

更改为然后重新编译,错误应该消失,也可以根据运行的脚本下载所需的任何更新。你的旧版本的构建工具,appcompat等。

并且不确定您的第一个代码段发生了什么,它不完整...