我正在尝试开始学习编写 Android 应用程序的技巧,因此我正在关注 Android 开发人员教程。我目前正在研究“开始另一个活动”部分,并且完全按照示例进行操作,但应用程序名称除外。当我为单击按钮时要调用的第二个活动添加类代码时,出现以下错误:缺少包语句:'com.VRCoreSoftware.testapp4'
所有相关的代码(据我所知)如下所示:
Main Activity.java:这里显示的唯一错误是它无法识别 DisplayMessageActivity.class Intent intent = new Intent(this, DisplayMessageActivity.class);
package com.VRCoreSoftware.testapp4;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
// import android.view.View;
import android.content.Intent;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.VRCoreSoftware.testapp4.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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.
switch (item.getItemId()) {
case 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_main, container, false);
return rootView;
}
}
public void sendMessage(View view) {
// Do something in response to button
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);
}
}
DisplayMessageActivity.java 有很多错误,源(我假设)是明显的失败导入。
import com.VRCoreSoftware.testapp4.MainActivity;
public class DisplayMessageActivity extends MainActivity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
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);
// Set the text view as the activity layout
setContentView(textView);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
来自 AndroidManifest.xml 文件:这里唯一的错误是它无法识别行中的“DisplayMessageActivity”:android:name="com.VRCoreSoftware.testapp4.DisplayMessageActivity"
<activity
android:name="com.VRCoreSoftware.testapp4.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.VRCoreSoftware.testapp4.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.VRCoreSoftware.testapp4.MainActivity" />
</activity>
我目前正在使用 Android Studio 编写和编译这段代码。对于此事,我将不胜感激。非常感谢。