我正在尝试开始学习编写Android应用程序的绳索,因此我正在关注Android开发人员教程。我目前正在开展“开始另一个活动”部分,并且已经完全跟踪了示例,但应用名称除外。当我为单击按钮时要调用的第二个活动添加类代码时,我收到以下错误:缺少包语句:'com.VRCoreSoftware.testapp4'
所有相关的代码片段(据我所知)如下所示:
主要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”: 机器人:名称= “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编写和编译此代码。 我非常感谢这件事的任何和所有帮助。 非常感谢你。
答案 0 :(得分:0)
似乎DisplayMessageActivity与MainActivity不在同一个包中,这也意味着它不在com.VRCoreSoftware.testapp4
中,使得清单错误。把它移到正确的包装上,你就可以了。