我正在关注Android开发者的“启动另一个活动”教程,并按照所有步骤操作,但是当我尝试按下按钮开始新活动时,我的应用程序仍然崩溃。这是我使用的代码和我的logcat。
package com.example.mysecondapp;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.mysecondapp.MESSAGE";
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);
Log.d("intent", "intentstarted");
startActivity(intent);
Log.d("activity", "activitty");
}
@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.
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_main, container, false);
return rootView;
}
}
}
package com.example.mysecondapp;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
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.widget.TextView;
import android.os.Build;
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// 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);
// Set the text view as the activity layout
setContentView(textView);
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.display_message, 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();
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;
}
}
}
log cat:
06-28 20:11:47.013: I/Process(308): Sending signal. PID: 308 SIG: 9
06-28 20:25:17.352: I/dalvikvm(336): Could not find method android.content.pm.PackageManager.getActivityLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
06-28 20:25:17.352: W/dalvikvm(336): VFY: unable to resolve virtual method 319: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
06-28 20:25:17.352: D/dalvikvm(336): VFY: replacing opcode 0x6e at 0x008b
06-28 20:25:17.352: I/dalvikvm(336): Could not find method android.content.pm.ApplicationInfo.loadLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
06-28 20:25:17.352: W/dalvikvm(336): VFY: unable to resolve virtual method 315: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
06-28 20:25:17.352: D/dalvikvm(336): VFY: replacing opcode 0x6e at 0x0099
06-28 20:25:17.372: D/dalvikvm(336): VFY: dead code 0x008e-0092 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
06-28 20:25:17.372: D/dalvikvm(336): VFY: dead code 0x009c-00a0 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
06-28 20:25:20.082: W/KeyCharacterMap(336): No keyboard for id 0
06-28 20:25:20.082: W/KeyCharacterMap(336): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
06-28 20:25:37.843: D/dalvikvm(336): GC_FOR_MALLOC freed 3702 objects / 228664 bytes in 201ms
06-28 20:25:37.863: D/intent(336): intentstarted
06-28 20:25:37.903: D/activity(336): activitty
06-28 20:25:38.393: E/FragmentManager(336): No view found for id 0x7f05003c (com.example.mysecondapp:id/container) for fragment PlaceholderFragment{45fa00f0 #0 id=0x7f05003c}
06-28 20:25:38.393: E/FragmentManager(336): Activity state:
06-28 20:25:38.404: D/FragmentManager(336): Local FragmentActivity 45f8a0b0 State:
06-28 20:25:38.413: D/FragmentManager(336): mCreated=truemResumed=false mStopped=false mReallyStopped=false
06-28 20:25:38.423: D/FragmentManager(336): mLoadersStarted=false
06-28 20:25:38.423: D/FragmentManager(336): Active Fragments in 45f776c8:
06-28 20:25:38.443: D/FragmentManager(336): #0: PlaceholderFragment{45fa00f0 #0 id=0x7f05003c}
06-28 20:25:38.443: D/FragmentManager(336): mFragmentId=#7f05003c mContainerId=#7f05003c mTag=null
06-28 20:25:38.453: D/FragmentManager(336): mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
06-28 20:25:38.463: D/FragmentManager(336): mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
06-28 20:25:38.463: D/FragmentManager(336): mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
06-28 20:25:38.483: D/FragmentManager(336): mRetainInstance=false mRetaining=false mUserVisibleHint=true
06-28 20:25:38.483: D/FragmentManager(336): mFragmentManager=FragmentManager{45f776c8 in DisplayMessageActivity{45f8a0b0}}
06-28 20:25:38.483: D/FragmentManager(336): mActivity=com.example.mysecondapp.DisplayMessageActivity@45f8a0b0
06-28 20:25:38.503: D/FragmentManager(336): Added Fragments:
06-28 20:25:38.503: D/FragmentManager(336): #0: PlaceholderFragment{45fa00f0 #0 id=0x7f05003c}
06-28 20:25:38.513: D/FragmentManager(336): FragmentManager misc state:
06-28 20:25:38.523: D/FragmentManager(336): mActivity=com.example.mysecondapp.DisplayMessageActivity@45f8a0b0
06-28 20:25:38.523: D/FragmentManager(336): mContainer=android.support.v4.app.FragmentActivity$2@45f550d0
06-28 20:25:38.542: D/FragmentManager(336): mCurState=2 mStateSaved=false mDestroyed=false
06-28 20:25:38.542: D/FragmentManager(336): View Hierarchy:
06-28 20:25:38.563: D/FragmentManager(336): com.android.internal.policy.impl.PhoneWindow$DecorView{45f87538 V.E..... ... 0,0-0,0}
06-28 20:25:38.563: D/FragmentManager(336): android.widget.FrameLayout{45f81e98 V.ED.... ... 0,0-0,0}
06-28 20:25:38.573: D/FragmentManager(336): android.widget.LinearLayout{45fa71b0 V.E..... ... 0,0-0,0}
06-28 20:25:38.583: D/FragmentManager(336): android.support.v7.internal.widget.ActionBarContainer{45f9d1e0 V.ED.... ... 0,0-0,0 #7f05001b app:id/action_bar_container}
06-28 20:25:38.583: D/FragmentManager(336): android.support.v7.internal.widget.ActionBarView{45f67418 V.E..... ... 0,0-0,0 #7f05001c app:id/action_bar}
06-28 20:25:38.603: D/FragmentManager(336): android.widget.LinearLayout{45fa99d0 G.....C. ... 0,0-0,0}
06-28 20:25:38.603: D/FragmentManager(336): android.widget.ImageView{45f7d428 G.ED.... ... 0,0-0,0 #7f050021 app:id/up}
06-28 20:25:38.603: D/FragmentManager(336): android.widget.LinearLayout{45f7c720 V.E..... ... 0,0-0,0}
06-28 20:25:38.623: D/FragmentManager(336): android.widget.TextView{45f5fa98 V.ED.... ... 0,0-0,0 #7f050022 app:id/action_bar_title}
06-28 20:25:38.623: D/FragmentManager(336): android.widget.TextView{45f622b8 G.ED.... ... 0,0-0,0 #7f050023 app:id/action_bar_subtitle}
06-28 20:25:38.633: D/FragmentManager(336): android.support.v7.internal.widget.ActionBarView$HomeView{45f700c8 VFE...C. ... 0,0-0,0}
06-28 20:25:38.633: D/FragmentManager(336): android.widget.ImageView{45f3ebf0 G.ED.... ... 0,0-0,0 #7f050021 app:id/up}
06-28 20:25:38.652: D/FragmentManager(336): android.widget.ImageView{45f769d0 V.ED.... ... 0,0-0,0 #7f050014 app:id/home}
06-28 20:25:38.652: D/FragmentManager(336): android.support.v7.internal.widget.ActionBarContextView{45fa1430 G.E..... ... 0,0-0,0 #7f05001d app:id/action_context_bar}
06-28 20:25:38.673: D/FragmentManager(336): android.widget.FrameLayout{45f92928 V.ED.... ... 0,0-0,0 #1020002 android:id/content}
06-28 20:25:38.673: D/FragmentManager(336): android.widget.TextView{45f64db0 V.ED.... ... 0,0-0,0}
06-28 20:25:38.683: D/FragmentManager(336): android.support.v7.internal.widget.ActionBarContainer{45f99900 G.ED.... ... 0,0-0,0 #7f05001e app:id/split_action_bar}
06-28 20:25:38.693: D/AndroidRuntime(336): Shutting down VM
06-28 20:25:38.693: W/dalvikvm(336): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-28 20:25:38.733: E/AndroidRuntime(336): FATAL EXCEPTION: main
06-28 20:25:38.733: E/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mysecondapp/com.example.mysecondapp.DisplayMessageActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.mysecondapp:id/container) for fragment PlaceholderFragment{45fa00f0 #0 id=0x7f05003c}
06-28 20:25:38.733: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-28 20:25:38.733: E/AndroidRuntime(336): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-28 20:25:38.733: E/AndroidRuntime(336): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-28 20:25:38.733: E/AndroidRuntime(336): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-28 20:25:38.733: E/AndroidRuntime(336): at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 20:25:38.733: E/AndroidRuntime(336): at android.os.Looper.loop(Looper.java:123)
06-28 20:25:38.733: E/AndroidRuntime(336): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-28 20:25:38.733: E/AndroidRuntime(336): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 20:25:38.733: E/AndroidRuntime(336): at java.lang.reflect.Method.invoke(Method.java:521)
06-28 20:25:38.733: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-28 20:25:38.733: E/AndroidRuntime(336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-28 20:25:38.733: E/AndroidRuntime(336): at dalvik.system.NativeStart.main(Native Method)
06-28 20:25:38.733: E/AndroidRuntime(336): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.mysecondapp:id/container) for fragment PlaceholderFragment{45fa00f0 #0 id=0x7f05003c}
06-28 20:25:38.733: E/AndroidRuntime(336): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
06-28 20:25:38.733: E/AndroidRuntime(336): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
06-28 20:25:38.733: E/AndroidRuntime(336): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-28 20:25:38.733: E/AndroidRuntime(336): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
06-28 20:25:38.733: E/AndroidRuntime(336): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
06-28 20:25:38.733: E/AndroidRuntime(336): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
06-28 20:25:38.733: E/AndroidRuntime(336): at android.app.Activity.performStart(Activity.java:3781)
06-28 20:25:38.733: E/AndroidRuntime(336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
06-28 20:25:38.733: E/AndroidRuntime(336): ... 11 more
06-28 20:25:41.702: I/Process(336): Sending signal. PID: 336 SIG: 9
答案 0 :(得分:0)
我认为你在第二个活动中出错了在第二个活动中移除了这行代码并且它应该可以工作
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
}
您正在尝试将PlaceholderFragment添加到R.id.container,当第二个活动的内容视图是没有该容器的TextView时,这有助于