我创建了一个ImageView
,希望通过单击它可以将我从一项活动转到另一项活动。
我输入了以下代码,但是编译器在说:“错误:找不到符号方法findViewById(int)”。如果您能帮助我解决这个问题,我将不胜感激。
我的代码:
package com.androidcodefinder.loginscreendemo.Catalysts;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.androidcodefinder.loginscreendemo.MainActivity;
import com.androidcodefinder.loginscreendemo.R;
import com.androidcodefinder.loginscreendemo.SignUpActivity;
import com.androidcodefinder.loginscreendemo.postpg;
public class AIFragment extends android.support.v4.app.Fragment {
private static final String TAG = "AIFragments";
private ImageView imageView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ai, container, false);
return view;
imageView=(ImageView)findViewById(R.id.savage);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),postpg.class);
startActivity(intent);
}
});
}
}
答案 0 :(得分:0)
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ai, container, false);
imageView=(ImageView)view.findViewById(R.id.savage);
imageView.setOnClickListener((view) -> {
Intent intent = new Intent(getActivity(),postpg.class);
startActivity(intent);
}
});
return view;
}
函数findViewById是在视图上定义的。使用您正在膨胀的视图。我还将click-Listener更改为lambda函数,因为它更易于阅读。
或使用旧语法:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ai, container, false);
imageView=(ImageView)view.findViewById(R.id.savage);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(),postpg.class);
startActivity(intent);
}
});
return view;
}