简体   繁体   中英

What am I doing wrong when I add my fragment to my activity?

So basically, whenever I launch my app, it pops up and then cancels. The purpose of the app is to click one of 3 buttons and it will set an ImageView to that picture, but i wanted to experiment with fragments. Any help would be appreciated.

Here is my main activity code

public class MainActivity extends AppCompatActivity implements awkwardfragment.hello {

ImageView Ryan;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });




}

@Override
public void changer(int a) {
    Ryan = (ImageView) findViewById(R.id.imageView);

    if(a == 1){
        Ryan.setImageResource(R.drawable.awkward_frog);
    }
    else if(a == 2){
        Ryan.setImageResource(R.drawable.awkward_giraffe);
    }
    else if(a==3){
        Ryan.setImageResource(R.drawable.awkward_octopus);

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

        return super.onOptionsItemSelected(item);
   }
}

Here is my xml code

<ImageView
    android:layout_width="500px"
    android:layout_height="400px"
    android:id="@+id/imageView"
    android:background="#ffffff"
    android:visibility="invisible"
    android:contentDescription="frog"
    android:layout_above="@+id/fragment"
    android:layout_centerHorizontal="true" />

<fragment
    android:layout_width="200px"
    android:layout_height="400px"
    android:name="com.hitchhikingapps.imageviewing.awkwardfragment"
    android:id="@+id/fragment"
    tools:layout="@layout/awkward"
    android:layout_marginBottom="35dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

And here is my java code

public class awkwardfragment extends Fragment{

hello activityCommander;

public interface hello{
    public void changer(int a);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    Activity a=(Activity) context;
    try{
        activityCommander = (hello) a;
    }catch(ClassCastException e){
        throw new ClassCastException(a.toString());
    }
}

Button frog,octopus,giraffe;
int count = 0;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.awkward,container,false);

    frog = (Button) getView().findViewById(R.id.frog);
    giraffe = (Button) getView().findViewById(R.id.giraffe);
    octopus = (Button) getView().findViewById(R.id.octopus);

   frog.setOnClickListener (
        new View.OnClickListener() {
            public void onClick(View view) {
                activityCommander.changer(1);
            }
        }
   );
    giraffe.setOnClickListener (
            new View.OnClickListener() {
                public void onClick(View view) {
                    activityCommander.changer(2);
                }
            }
    );

    octopus.setOnClickListener (
            new View.OnClickListener() {
                public void onClick(View view) {
                    activityCommander.changer(3);
                }
            }
    );

    return view;
}

}

And lastly, my XML code

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="frog"
    android:id="@+id/frog"
    android:layout_marginBottom="126dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:onClick="fro" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="giraffe"
    android:id="@+id/giraffe"
    android:layout_alignTop="@+id/frog"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:onClick="gir" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="octopus"
    android:id="@+id/octopus"
    android:layout_below="@+id/giraffe"
    android:layout_centerHorizontal="true"
    android:onClick="oct" />

FloatingActioncode

 <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

Change getView to view. getView() == null at this point.

    frog = (Button) view.findViewById(R.id.frog);
    giraffe = (Button) view.findViewById(R.id.giraffe);
    octopus = (Button) view.findViewById(R.id.octopus);

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