当我尝试访问在视图类中定义的文本视图时,我收到NullPointerException。我从设置类访问它。我的代码的一小部分是:
查看课程
public class view1 extends menu {
public static TextView text1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
text1=(TextView)findViewById(R.id.textfile1);
text1.setText("product");
}
public void small(String mytext) { // this is my method which I want to access
text1.setText(mytext);
}
}
设置课程
public class Setting extends Activity {
private Spinner spinner1;
private Button apply;
TextView small1;
private view1 view11;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
//setContentView(R.layout.view);
addItemsOnSpinner1();
addListenerOnSpinnerItemSelection();
}
public void addItemsOnSpinner1() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("Small");
list.add("Medium");
list.add("Large");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
apply = (Button) findViewById(R.id.apply);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
apply.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String mytext = "Something else";
view11.small(mytext);
}
堆栈跟踪
01-17 00:11:02.064: E/AndroidRuntime(4191): FATAL EXCEPTION: main
01-17 00:11:02.064: E/AndroidRuntime(4191): java.lang.NullPointerException
01-17 00:11:02.064: E/AndroidRuntime(4191): at com.ramanrayat.notelet.Setting$1.onClick(Setting.java:106)
01-17 00:11:02.064: E/AndroidRuntime(4191): at android.view.View.performClick(View.java:4240)
01-17 00:11:02.064: E/AndroidRuntime(4191): at android.view.View$PerformClick.run(View.java:17721)
01-17 00:11:02.064: E/AndroidRuntime(4191): at android.os.Handler.handleCallback(Handler.java:730)
01-17 00:11:02.064: E/AndroidRuntime(4191): at android.os.Handler.dispatchMessage(Handler.java:92)
01-17 00:11:02.064: E/AndroidRuntime(4191): at android.os.Looper.loop(Looper.java:137)
01-17 00:11:02.064: E/AndroidRuntime(4191): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-17 00:11:02.064: E/AndroidRuntime(4191): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 00:11:02.064: E/AndroidRuntime(4191): at java.lang.reflect.Method.invoke(Method.java:525)
01-17 00:11:02.064: E/AndroidRuntime(4191): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-17 00:11:02.064: E/AndroidRuntime(4191): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-17 00:11:02.064: E/AndroidRuntime(4191): at dalvik.system.NativeStart.main(Native Method)
setting.xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="Setting"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="40dp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:gravity="center"
android:text="Font Size"
android:textSize="30dp" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="30dp" />
<Button
android:id="@+id/apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_alignRight="@+id/textView2"
android:layout_below="@+id/spinner1"
android:layout_marginTop="58dp"
android:text="Apply" />
</RelativeLayout>
答案 0 :(得分:0)
Chnage
if(String.valueOf(spinner1.getSelectedItem())=="Small")
要
if(String.valueOf(spinner1.getSelectedItem()).equals("Small"))
使用.equals
或.equalsIgnoreCase
来比较字符串
不应使textview静态,而应使用意图在活动之间传递值。
Intent intent = new Intent(ActivityName.this,Settings.class);
intent.putExtra("key",text1.getText().toString());
startActivity(intent);
然后
String value = getIntent().getStringExtra("key");
更改
public static TextView text1;
到
public TextView text1;
也遵循java命名约定
答案 1 :(得分:0)
替换
public static TextView text1;
与
TextView text1;
text1不能是静态的。
下次,请单击错误日志中的文件名,并指明代码清单中的哪个行号是错误实际指向的行。
学会用大写字母开始所有的课程名称(尽管如此,这不是导致问题的原因)。虽然我喜欢它,但请停止在类名和变量中使用数字,特别是数字1,在某些字体中可能会被模糊地读作“l”。
答案 2 :(得分:0)
找你的LogCat,它会告诉你NPE在哪里 另一个问题是:不要使用equals来比较String而不是==
==比较两个对象的地址,等于比较它们的值。
if(String.valueOf(spinner1.getSelectedItem()).equals("Small")) {
String mytext = "Something else ";
view11.small(mytext); // view11 is clas view1 reference
}
else if(String.valueOf(spinner1.getSelectedItem()).equals("Medium")) {
finish();
}