简体   繁体   中英

Android Application keeps crashing [Eclipse]

I have this code :

public class MainActivity extends ActionBarActivity   {
    TextView tv;
    Button b1;
    String answer;
    RadioButton r1,r2,r3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        tv = (TextView) findViewById(R.id.textView1);
        b1 =(Button) findViewById(R.id.button1);
        r1 = (RadioButton) findViewById(R.id.radioButton1);
        r2 = (RadioButton) findViewById(R.id.radioButton2);
        r3 = (RadioButton) findViewById(R.id.radioButton3);        
        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (r1.isChecked() == true) {
                    answer=null;

                }else if (r2.isChecked() == true){
                    answer=null;
                }else if (r3.isChecked() == true){
                    answer="correct";
                }else{
                    answer="incorrect";
                }
                Intent i = new Intent(MainActivity.this , receiver.class);
                 i.putExtra("key" , answer);
                  startActivity(i);

            }
        });

but the problem is that the application keeps crashing upon startup . Here's my LogCat data :

04-03 19:56:45.955: I/dalvikvm(620): Could not find method android.content.pm.PackageManager.getActivityLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-03 19:56:45.955: W/dalvikvm(620): VFY: unable to resolve virtual method 320: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
04-03 19:56:45.955: D/dalvikvm(620): VFY: replacing opcode 0x6e at 0x008b
04-03 19:56:45.955: I/dalvikvm(620): Could not find method android.content.pm.ApplicationInfo.loadLogo, referenced from method android.support.v7.internal.widget.ActionBarView.<init>
04-03 19:56:45.955: W/dalvikvm(620): VFY: unable to resolve virtual method 316: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
04-03 19:56:45.955: D/dalvikvm(620): VFY: replacing opcode 0x6e at 0x0099
04-03 19:56:45.965: D/dalvikvm(620): VFY: dead code 0x008e-0092 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-03 19:56:45.965: D/dalvikvm(620): VFY: dead code 0x009c-00a0 in Landroid/support/v7/internal/widget/ActionBarView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;)V
04-03 19:56:46.115: E/FragmentManager(620): No view found for id 0x7f05003c (com.example.radiogroup:id/container) for fragment PlaceholderFragment{4a312d58 #0 id=0x7f05003c}
04-03 19:56:46.115: E/FragmentManager(620): Activity state:
04-03 19:56:46.115: D/FragmentManager(620):   Local FragmentActivity 4a2f4a18 State:
04-03 19:56:46.115: D/FragmentManager(620):     mCreated=truemResumed=false mStopped=false mReallyStopped=false
04-03 19:56:46.115: D/FragmentManager(620):     mLoadersStarted=false
04-03 19:56:46.115: D/FragmentManager(620):   Active Fragments in 4a2f59e8:
04-03 19:56:46.125: D/FragmentManager(620):     #0: PlaceholderFragment{4a312d58 #0 id=0x7f05003c}
04-03 19:56:46.125: D/FragmentManager(620):       mFragmentId=#7f05003c mContainerId=#7f05003c mTag=null
04-03 19:56:46.125: D/FragmentManager(620):       mState=0 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
04-03 19:56:46.125: D/FragmentManager(620):       mAdded=true mRemoving=false mResumed=false mFromLayout=false mInLayout=false
04-03 19:56:46.125: D/FragmentManager(620):       mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
04-03 19:56:46.125: D/FragmentManager(620):       mRetainInstance=false mRetaining=false mUserVisibleHint=true
04-03 19:56:46.136: D/FragmentManager(620):       mFragmentManager=FragmentManager{4a2f59e8 in MainActivity{4a2f4a18}}
04-03 19:56:46.136: D/FragmentManager(620):       mActivity=com.example.radiogroup.MainActivity@4a2f4a18
04-03 19:56:46.136: D/FragmentManager(620):   Added Fragments:
04-03 19:56:46.136: D/FragmentManager(620):     #0: PlaceholderFragment{4a312d58 #0 id=0x7f05003c}
04-03 19:56:46.136: D/FragmentManager(620):   FragmentManager misc state:
04-03 19:56:46.136: D/FragmentManager(620):     mActivity=com.example.radiogroup.MainActivity@4a2f4a18
04-03 19:56:46.145: D/FragmentManager(620):     mContainer=android.support.v4.app.FragmentActivity$2@4a2f62f0
04-03 19:56:46.145: D/FragmentManager(620):     mCurState=2 mStateSaved=false mDestroyed=false
04-03 19:56:46.145: D/FragmentManager(620):   View Hierarchy:
04-03 19:56:46.145: D/FragmentManager(620):     com.android.internal.policy.impl.PhoneWindow$DecorView{4a2f9580 V.E..... ... 0,0-0,0}
04-03 19:56:46.145: D/FragmentManager(620):       android.widget.FrameLayout{4a2f9ec0 V.ED.... ... 0,0-0,0}
04-03 19:56:46.145: D/FragmentManager(620):         android.widget.LinearLayout{4a2fabe0 V.E..... ... 0,0-0,0}
04-03 19:56:46.155: D/FragmentManager(620):           android.support.v7.internal.widget.ActionBarContainer{4a2fb678 V.ED.... ... 0,0-0,0 #7f05001b app:id/action_bar_container}
04-03 19:56:46.155: D/FragmentManager(620):             android.support.v7.internal.widget.ActionBarView{4a304040 V.E..... ... 0,0-0,0 #7f05001c app:id/action_bar}
04-03 19:56:46.155: D/FragmentManager(620):               android.widget.LinearLayout{4a308948 G.....C. ... 0,0-0,0}
04-03 19:56:46.155: D/FragmentManager(620):                 android.widget.ImageView{4a309298 G.ED.... ... 0,0-0,0 #7f050021 app:id/up}
04-03 19:56:46.155: D/FragmentManager(620):                 android.widget.LinearLayout{4a3094c8 V.E..... ... 0,0-0,0}
04-03 19:56:46.155: D/FragmentManager(620):                   android.widget.TextView{4a309948 V.ED.... ... 0,0-0,0 #7f050022 app:id/action_bar_title}
04-03 19:56:46.165: D/FragmentManager(620):                   android.widget.TextView{4a30ab88 G.ED.... ... 0,0-0,0 #7f050023 app:id/action_bar_subtitle}
04-03 19:56:46.165: D/FragmentManager(620):               android.support.v7.internal.widget.ActionBarView$HomeView{4a304f38 VFE...C. ... 0,0-0,0}
04-03 19:56:46.165: D/FragmentManager(620):                 android.widget.ImageView{4a307238 G.ED.... ... 0,0-0,0 #7f050021 app:id/up}
04-03 19:56:46.165: D/FragmentManager(620):                 android.widget.ImageView{4a307798 V.ED.... ... 0,0-0,0 #7f050014 app:id/home}
04-03 19:56:46.165: D/FragmentManager(620):             android.support.v7.internal.widget.ActionBarContextView{4a30b558 G.E..... ... 0,0-0,0 #7f05001d app:id/action_context_bar}
04-03 19:56:46.175: D/FragmentManager(620):           android.widget.FrameLayout{4a30bef0 V.ED.... ... 0,0-0,0 #1020002 android:id/content}
04-03 19:56:46.175: D/FragmentManager(620):             android.widget.RelativeLayout{4a30d508 V.E..... ... 0,0-0,0}
04-03 19:56:46.175: D/FragmentManager(620):               android.widget.RadioGroup{4a30dc08 V.E..... ... 0,0-0,0 #7f05003d app:id/radioGroup1}
04-03 19:56:46.175: D/FragmentManager(620):               android.widget.TextView{4a30e108 V.ED.... ... 0,0-0,0 #7f05003e app:id/textView1}
04-03 19:56:46.175: D/FragmentManager(620):               android.widget.Button{4a30e878 VFED..C. ... 0,0-0,0 #7f05003f app:id/button1}
04-03 19:56:46.175: D/FragmentManager(620):               android.widget.RadioButton{4a30f470 VFED..C. ... 0,0-0,0 #7f050040 app:id/radioButton1}
04-03 19:56:46.185: D/FragmentManager(620):               android.widget.RadioButton{4a310170 VFED..C. ... 0,0-0,0 #7f050041 app:id/radioButton2}
04-03 19:56:46.185: D/FragmentManager(620):               android.widget.RadioButton{4a310b10 VFED..C. ... 0,0-0,0 #7f050042 app:id/radioButton3}
04-03 19:56:46.185: D/FragmentManager(620):           android.support.v7.internal.widget.ActionBarContainer{4a30c198 G.ED.... ... 0,0-0,0 #7f05001e app:id/split_action_bar}
04-03 19:56:46.185: D/AndroidRuntime(620): Shutting down VM
04-03 19:56:46.185: W/dalvikvm(620): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-03 19:56:46.196: E/AndroidRuntime(620): FATAL EXCEPTION: main
04-03 19:56:46.196: E/AndroidRuntime(620): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.radiogroup/com.example.radiogroup.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.radiogroup:id/container) for fragment PlaceholderFragment{4a312d58 #0 id=0x7f05003c}
04-03 19:56:46.196: E/AndroidRuntime(620):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-03 19:56:46.196: E/AndroidRuntime(620):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-03 19:56:46.196: E/AndroidRuntime(620):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-03 19:56:46.196: E/AndroidRuntime(620):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-03 19:56:46.196: E/AndroidRuntime(620):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-03 19:56:46.196: E/AndroidRuntime(620):  at android.os.Looper.loop(Looper.java:123)
04-03 19:56:46.196: E/AndroidRuntime(620):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-03 19:56:46.196: E/AndroidRuntime(620):  at java.lang.reflect.Method.invokeNative(Native Method)
04-03 19:56:46.196: E/AndroidRuntime(620):  at java.lang.reflect.Method.invoke(Method.java:521)
04-03 19:56:46.196: E/AndroidRuntime(620):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-03 19:56:46.196: E/AndroidRuntime(620):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-03 19:56:46.196: E/AndroidRuntime(620):  at dalvik.system.NativeStart.main(Native Method)
04-03 19:56:46.196: E/AndroidRuntime(620): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f05003c (com.example.radiogroup:id/container) for fragment PlaceholderFragment{4a312d58 #0 id=0x7f05003c}
04-03 19:56:46.196: E/AndroidRuntime(620):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:930)
04-03 19:56:46.196: E/AndroidRuntime(620):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
04-03 19:56:46.196: E/AndroidRuntime(620):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
04-03 19:56:46.196: E/AndroidRuntime(620):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
04-03 19:56:46.196: E/AndroidRuntime(620):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
04-03 19:56:46.196: E/AndroidRuntime(620):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
04-03 19:56:46.196: E/AndroidRuntime(620):  at android.app.Activity.performStart(Activity.java:3781)
04-03 19:56:46.196: E/AndroidRuntime(620):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
04-03 19:56:46.196: E/AndroidRuntime(620):  ... 11 more

So what is the possible reason of this error ? And thanks in advance . I apologize for my weak language .

Update : fragment_main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.radiogroup.MainActivity$PlaceholderFragment" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="82dp"
        android:layout_marginTop="136dp" >
    </RadioGroup>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="21dp"
        android:text="Which Vitamin helps clotting the blood ?"
        android:textSize="30sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioGroup1"
        android:layout_below="@+id/radioGroup1"
        android:layout_marginTop="102dp"
        android:text="Send"
        android:textSize="30sp" />

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/radioGroup1"
        android:layout_alignRight="@+id/button1"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioGroup1"
        android:layout_below="@+id/radioGroup1"
        android:layout_marginTop="16dp"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/radioButton2"
        android:text="RadioButton" />

</RelativeLayout>

Looks like you've changed setContentView(R.layout.activity_main) to setContentView(R.layout.fragment_main) but there's still the template-generated PlaceholderFragment transaction that attempts to put the fragment in view R.id.container in the main activity layout and the view is not found.

Either remove the transaction, or use fragment layouts the way the template wants you to: separate activity and fragment layouts and setting up listeners etc. in fragment onCreateView() . See NullPointerException accessing views in onCreate() for more on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM