简体   繁体   中英

Crashing app due to No view found

I have seen so much answer related to this but coudlnt find my solution.My app is crashing due to no view found for id 0x7f080058 for fragment LoginFragment. I have tried soo much solutions provided already over here but couldnt find any which solve out my problem so plz give some explicit solution.

Main Activity Code :

package com.vshine.neuron.riseshine;

import android.app.Fragment;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.PrefConfig;


public class MainActivity extends AppCompatActivity implements LoginFragment.OnLoginFormActivityListener{
    Button story,poem,puzle,quz,uncle_shine,joke,vlb,vkitchen,young_writer;
    public static PrefConfig prefConfig;

    LinearLayout linearLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       prefConfig = new PrefConfig(this);

       if (findViewById(R.id.fragment_container)!=null){
           if(savedInstanceState!=null)
           {
               return;
           }
       }
       if(prefConfig.readLoginStatus())
       {
           getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,new WelcomeFragment()).commit();
       }
       else
       {
           getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,new LoginFragment()).commit();
       }

        story=  findViewById(R.id.stories);
        poem=  findViewById(R.id.poems);
        puzle=  findViewById(R.id.puzzle);
        quz=  findViewById(R.id.quiz);
        uncle_shine=  findViewById(R.id.ushine);
        joke=  findViewById(R.id.jokes);
        vlb= findViewById(R.id.vlab);
        vkitchen= findViewById(R.id.vkitchen);
        young_writer=  findViewById(R.id.vwriter);


        story.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getApplicationContext(),Story.class);
                startActivity(it);
            }
        });

        poem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it = new Intent(getApplicationContext(),poems.class);
                startActivity(it);
            }
        });

    puzle.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent it = new Intent(getApplicationContext(),puzzle.class);
            startActivity(it);

        }
    });

  quz.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Intent it = new Intent(getApplicationContext(),quiz.class);
          startActivity(it);
      }
  });

  uncle_shine.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Intent it = new Intent(getApplicationContext(),Uncleshine.class);
          startActivity(it);

      }
  });
  joke.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Intent it = new Intent(getApplicationContext(),joke.class);
          startActivity(it);
      }
  });
  vlb.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Intent it = new Intent(getApplicationContext(),vlab.class);
          startActivity(it);


      }
  });

  vkitchen.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
          Intent it = new Intent(getApplicationContext(),vkitchen.class);
          startActivity(it);

      }
  });
   young_writer.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {

       }
   });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // handle arrow click here
        if (item.getItemId() == android.R.id.home) {
            finish(); // close this activity and return to preview activity (if there is any)
        }

        return super.onOptionsItemSelected(item);

    }

    @Override
    public void performRegister()
    {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new RegistrationFragment()).addToBackStack(null).commit();
    }

    @Override
    public void performLogin(String name) {

    }
}

LoginFragment code:

 package com.vshine.neuron.riseshine;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends Fragment {

    private ImageButton RegText;
    OnLoginFormActivityListener loginFormActivityListener;
    public interface OnLoginFormActivityListener
    {
        public void performRegister();

        public void performLogin(String name);
    }

    public LoginFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_login, container, false);
        RegText = view.findViewById(R.id.register);
       RegText.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v)
           {
            loginFormActivityListener.performRegister();
           }
       });

        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Activity activity = (Activity) context;
        loginFormActivityListener = (OnLoginFormActivityListener) activity;
    }

}

XML Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:weightSum="4"
    android:layout_marginTop="-10dp"
    android:background="@drawable/background">
    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"></View>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_marginTop="10dp">

        <ImageView
            android:layout_width="46dp"
            android:layout_height="40dp"
            android:src="@drawable/login_user"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="15dp"/>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/etunameLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:passwordToggleEnabled="true">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/etuname"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Username"
                android:inputType="text"
                android:layout_marginRight="30dp"/>
        </android.support.design.widget.TextInputLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_marginTop="-60dp">

        <ImageView
            android:layout_width="46dp"
            android:layout_height="40dp"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="13dp"
            android:src="@drawable/login_pass" />

        <android.support.design.widget.TextInputLayout
            android:id="@+id/etPasswordLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:passwordToggleEnabled="true">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/etPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Password"
                android:inputType="textPassword"
                android:layout_marginRight="30dp"/>
        </android.support.design.widget.TextInputLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginTop="-50dip"
        android:orientation="vertical">

        <Button
            android:id="@+id/login_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/text_sty"
            android:fontFamily="comic-sans-ms"

            android:textColor="#ffffff"
            android:layout_marginRight="30dp"
            android:layout_marginLeft="35dp"
            android:text="Login"/>

        <ImageButton
            android:id="@+id/register"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_marginLeft="170dp"
            android:background="@drawable/signup"
            />

    </LinearLayout>



</LinearLayout>


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;


/**
 * A simple {@link Fragment} subclass.
 */
public class LoginFragment extends Fragment {

    private ImageButton RegText;
    OnLoginFormActivityListener loginFormActivityListener;
    public interface OnLoginFormActivityListener
    {
        public void performRegister();

        public void performLogin(String name);
    }

    public LoginFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_login, container, false);
        RegText = view.findViewById(R.id.register);
       RegText.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v)
           {
            loginFormActivityListener.performRegister();
           }
       });

        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Activity activity = (Activity) context;
        loginFormActivityListener = (OnLoginFormActivityListener) activity;
    }

}

Here is error log :

07-26 17:55:39.749 13946-13953/? E/art: Failed writing handshake bytes (-1 of 14): Broken pipe
07-26 17:55:41.100 13946-13946/com.vshine.neuron.riseshine E/MultiWindowProxy: getServiceInstance failed!
07-26 17:55:41.721 13946-13946/com.vshine.neuron.riseshine E/FragmentManager: No view found for id 0x7f080058 (com.vshine.neuron.riseshine:id/fragment_container) for fragment LoginFragment{96ca621 #0 id=0x7f080058}
    Activity state:
07-26 17:55:41.749 13946-13946/com.vshine.neuron.riseshine E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.vshine.neuron.riseshine, PID: 13946
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vshine.neuron.riseshine/com.vshine.neuron.riseshine.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f080058 (com.vshine.neuron.riseshine:id/fragment_container) for fragment LoginFragment{96ca621 #0 id=0x7f080058}
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2572)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:5728)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
     Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f080058 (com.vshine.neuron.riseshine:id/fragment_container) for fragment LoginFragment{96ca621 #0 id=0x7f080058}
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1422)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3248)
        at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3200)
        at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:195)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:597)
        at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1245)
        at android.app.Activity.performStart(Activity.java:6384)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2529)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2654) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:207) 
        at android.app.ActivityThread.main(ActivityThread.java:5728) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 

In your java code you are calling a view element that has an id of fragment_container .

Check your XML file (activity or fragment) and look to see if you misspelled this ID, or you may have removed it from the XML.

Fix: Add a view in your XML with that id, or removed the code in your class file looking for that id.

Sidenote: If you actually removed this from your java code, make sure you clean your project (Build -> Clean) and try to re-run it.

May your xml file version is limited...

在此处输入图片说明

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