我正在尝试制作一个按钮,计算点击次数并将它们显示在文本视图中。尝试了我所知道的一切。XML:
<LinearLayout
android:id="@+id/gpulay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="20dp" >
<TextView
android:id="@+id/master_control_text_gpulay"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Master Control :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/plus_gpulay"
android:layout_width="35dp"
android:layout_height="35dip"/>
</LinearLayout>
这是我的代码:
public class Main extends Fragment {
int c=0;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
ListView GPU_LAYOUT = (ListView)inflater.inflate(R.layout.gpu, container, false);
TextView text = (TextView) GPU_LAYOUT.findViewById(R.id.text1);
Button plus = (Button) GPU_LAYOUT.findViewById(R.id.butt1);
plus.setOnClickListener(new OnClickListener() { //null pointer this line, but it's from .srtText i think
public void onClick(View v) {
c++;
text.setText(c);
}
});
}
return GPU_LAYOUT;
}
当我打开它时应用程序强制关闭,所以我什至看不到主布局。