所以基本上,每当我启动我的应用程序时,它会弹出然后取消。该应用程序的目的是单击3个按钮之一,它将ImageView设置为该图片,但我想尝试片段。任何帮助将不胜感激。
这是我的主要活动代码
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);
}
}
这是我的xml代码
<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" />
这是我的java代码
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;
}
}
最后,我的XML代码
<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" />
答案 0 :(得分:1)
更改getView以查看。此时getView()== null。
frog = (Button) view.findViewById(R.id.frog);
giraffe = (Button) view.findViewById(R.id.giraffe);
octopus = (Button) view.findViewById(R.id.octopus);