所以我关注这个tutorial,使用Sublime作为文本编辑器并从控制台编译所有内容。
一切都很好,但是当我们来到你应该做第二次活动的部分时。他们使用Eclipse自动生成它并告诉我将其粘贴下来,所以我做到了。
这是我的代码(我在开头添加了一些导入和包,它修复了一些问题)
package pl.qnsi.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.MenuItem;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() { }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_display_message,
container, false);
return rootView;
}
}
}
[qnsi@archie MyFirstApp]$ ant debug
Buildfile: /home/qnsi/code/Apps/learning/MyFirstApp/build.xml
-set-mode-check:
-set-debug-files:
-check-env:
[checkenv] Android SDK Tools Revision 22.6.4
[checkenv] Installed at /opt/android-sdk
-setup:
[echo] Project Name: MyFirstApp
[gettype] Project Type: Application
-set-debug-mode:
-debug-obfuscation-check:
-pre-build:
-build-setup:
[getbuildtools] Using latest Build Tools: 19.1.0
[echo] Resolving Build Target for MyFirstApp...
[gettarget] Project Target: Android 4.4.2
[gettarget] API level: 19
[echo] ----------
[echo] Creating output directories if needed...
[echo] ----------
[echo] Resolving Dependencies for MyFirstApp...
[dependency] Library dependencies:
[dependency] No Libraries
[dependency]
[dependency] ------------------
[echo] ----------
[echo] Building Libraries with 'debug'...
[subant] No sub-builds to iterate on
-code-gen:
[mergemanifest] No changes in the AndroidManifest files.
[echo] Handling aidl files...
[aidl] No AIDL files to compile.
[echo] ----------
[echo] Handling RenderScript files...
[echo] ----------
[echo] Handling Resources...
[aapt] No changed resources. R.java and Manifest.java untouched.
[echo] ----------
[echo] Handling BuildConfig class...
[buildconfig] No need to generate new BuildConfig.
-pre-compile:
-compile:
[javac] Compiling 3 source files to /home/qnsi/code/Apps/learning/MyFirstApp/bin/classes
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:12: error: cannot find symbol
[javac] public class DisplayMessageActivity extends ActionBarActivity {
[javac] ^
[javac] symbol: class ActionBarActivity
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:40: error: cannot find symbol
[javac] public static class PlaceholderFragment extends Fragment {
[javac] ^
[javac] symbol: class Fragment
[javac] location: class DisplayMessageActivity
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:45: error: cannot find symbol
[javac] public View onCreateView(LayoutInflater inflater, ViewGroup container,
[javac] ^
[javac] symbol: class LayoutInflater
[javac] location: class PlaceholderFragment
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:45: error: cannot find symbol
[javac] public View onCreateView(LayoutInflater inflater, ViewGroup container,
[javac] ^
[javac] symbol: class ViewGroup
[javac] location: class PlaceholderFragment
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:45: error: cannot find symbol
[javac] public View onCreateView(LayoutInflater inflater, ViewGroup container,
[javac] ^
[javac] symbol: class View
[javac] location: class PlaceholderFragment
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:16: error: cannot find symbol
[javac] super.onCreate(savedInstanceState);
[javac] ^
[javac] symbol: variable super
[javac] location: class DisplayMessageActivity
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:17: error: cannot find symbol
[javac] setContentView(R.layout.activity_display_message);
[javac] ^
[javac] symbol: variable activity_display_message
[javac] location: class layout
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:21: error: cannot find symbol
[javac] .add(R.id.container, new PlaceholderFragment()).commit();
[javac] ^
[javac] symbol: variable container
[javac] location: class id
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:20: error: cannot find symbol
[javac] getSupportFragmentManager().beginTransaction()
[javac] ^
[javac] symbol: method getSupportFragmentManager()
[javac] location: class DisplayMessageActivity
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:14: error: method does not override or implement a method from a supertype
[javac] @Override
[javac] ^
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:31: error: cannot find symbol
[javac] if (id == R.id.action_settings) {
[javac] ^
[javac] symbol: variable action_settings
[javac] location: class id
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:34: error: cannot find symbol
[javac] return super.onOptionsItemSelected(item);
[javac] ^
[javac] symbol: variable super
[javac] location: class DisplayMessageActivity
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:25: error: method does not override or implement a method from a supertype
[javac] @Override
[javac] ^
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:47: error: cannot find symbol
[javac] View rootView = inflater.inflate(R.layout.fragment_display_message,
[javac] ^
[javac] symbol: class View
[javac] location: class PlaceholderFragment
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:47: error: cannot find symbol
[javac] View rootView = inflater.inflate(R.layout.fragment_display_message,
[javac] ^
[javac] symbol: variable fragment_display_message
[javac] location: class layout
[javac] /home/qnsi/code/Apps/learning/MyFirstApp/src/pl/qnsi/myfirstapp/DisplayMessageActivity.java:44: error: method does not override or implement a method from a supertype
[javac] @Override
[javac] ^
[javac] 16 errors
BUILD FAILED
/opt/android-sdk/tools/ant/build.xml:720: The following error occurred while executing this line:
/opt/android-sdk/tools/ant/build.xml:734: Compile failed; see the compiler error output for details.
Total time: 1 second
我搜索了一些答案,但大多数错误都是通过导入我已经导入的库来修复的。我从来没有看到过载方法的问题或没有找到超级方法。 我无能为力。
答案 0 :(得分:0)
如果您使用Eclipse添加DisplayMessageActivity.java
,那么它派生自ActionBarActivity
,并添加与您相关的所有支持代码。
但是,如果您使用命令行工具关注本教程,那么ActionBarActivity
内容尚未设置。他们在本教程的下一部分中进行了讨论。
相反,您可以将以下代码用于DisplayMessageActivity.java
:
package com.example.yourname;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.widget.EditText;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
}
当您执行步骤时,您还需要添加MainActivity.java
"启动第二个活动":
import android.view.View;
答案 1 :(得分:0)
在我成功构建之前,它确实有很大帮助。 他们还把我与MainActivity混淆了,有时指的是MyActivity。 所以无论如何DisplayMessageActivity.java文件如上所示,为了成功,我有MyActivity.java代码,如下所示:
package com.example.myfirstapp;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
public class MyActivity extends Activity {
//public class MyActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
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);
startActivity(intent);
}
}
现在我可以运行我的apk: - )