2015-11-30 36 views
0

我一直在使用Android Studio創建我的第一個應用程序。我沒有使用另一個IDE,以爲我在以下方面遇到了錯誤。Android Studio Basic的第一個練習

  • 我的動態
  • 顯示消息活動
  • 搖籃

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() 
    } 
    } 

我在活動上覆蓋和顯示信息活動公共無效和顯示信息錯誤。

+0

請仔細檢查你的代碼的格式。 –

+1

謝謝你的幫助 – Coder

回答

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

變化然後重新編譯,並且錯誤應該消失,還可以根據此腳本運行下載所需的任何更新。您對舊版本的編譯工具,程序兼容性等

而且不知道是怎麼回事你的第一個代碼片段,它是不完整的......

+0

謝謝你的幫助 – Coder