Java 按钮不';不能在android中工作
我正在开发一个应用程序,我有以下代码:Java 按钮不';不能在android中工作,java,android,button,onclick,android-inflate,Java,Android,Button,Onclick,Android Inflate,我正在开发一个应用程序,我有以下代码: package com.S.A.Productions.android.first; import com.S.A.Productions.android.first.R; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.SharedPreferences; impo
package com.S.A.Productions.android.first;
import com.S.A.Productions.android.first.R;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
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.Button;
import android.widget.TextView;
public class FirstActivity extends Fragment implements OnClickListener {
int counter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreate(savedInstanceState);
View v = inflater.inflate(R.layout.lin, container, false);
TextView temp = (TextView) v.findViewById(R.id.textView2);
//Set the buttons
Button button2 = (Button) v.findViewById(R.id.button2);
//+++ BUTTON
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub//Get content of TextView
TextView temp = (TextView) v.findViewById(R.id.textView2);
//Convert the string to an integer
counter = Integer.parseInt(temp.getText().toString());
counter++;
temp.setText("" + counter);
String stringData = temp.getText().toString();
SharedPreferences.Editor editor = someData.edit();
editor.putString("sharedString", stringData);
editor.commit();
}
});
//END OF +++ BUTTON
return v;
}
}
但当我运行应用程序并点击该按钮时,应用程序崩溃。
我用的是“v.findviewbyd”
最后我返回v。所以我不知道到底出了什么问题。
有什么想法吗?onClick(视图v)的参数v是单击的元素,在您的例子中是一个
按钮
错误在于,通过尝试从按钮本身按Id查找文本视图,您正在使用参数V(它是按钮
)作为视图组。textView
将为空。setText()
将导致崩溃
您需要在视图层次结构中的textView
的任何容器上使用findviewbyid()
。冲突是因为onclick内的视图v和膨胀视图v不同。此代码将在以下情况下工作:
public class FirstActivity extends Fragment implements OnClickListener {
int counter;
View v ;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreate(savedInstanceState);
v = inflater.inflate(R.layout.lin, container, false);
TextView temp = (TextView) v.findViewById(R.id.textView2);
//Set the buttons
Button button2 = (Button) v.findViewById(R.id.button2);
//+++ BUTTON
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub//Get content of TextView
TextView temp = (TextView) this.v.findViewById(R.id.textView2);
//Convert the string to an integer
counter = Integer.parseInt(temp.getText().toString());
counter++;
temp.setText("" + counter);
String stringData = temp.getText().toString();
SharedPreferences.Editor editor = someData.edit();
editor.putString("sharedString", stringData);
editor.commit();
}
});
//END OF +++ BUTTON
return v;
}
}
你能发布跟踪和布局吗?我明白。我刚把“v”改为“myView”,现在一切都正常了!非常感谢你,我已经试了好几个小时了。我没有足够的代表:(你可以在没有代表的情况下“向上投票”和“勾选”你问题的答案。