我只想通过单击按钮来显示文本,但我不知道为什么我会得到空指针异常。为什么View无法在我的案例中找到任何想法?
public class MainActivity extends Activity {
// Button button;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
Random random = new Random(100);
// View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
tv = (TextView) findViewById(R.id.textView);
tv.setText("change");
}
});
// tv=(TextView)findViewById(R.id.textView);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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);
}
}
我已经在activity_main.xml中正确声明了所有id,但它在结果中给出了空指针:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press"
android:id="@+id/but"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_above="@+id/but"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:text="100" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="press me"
android:id="@+id/button"
android:layout_above="@+id/but"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
出现以下错误:
Process: com.example.admins.assignment1, PID: 23476
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admins.assignment1/com.example.admins.assignment1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
enter code here
答案 0 :(得分:4)
你必须在调用任何方法之前设置视图,所以移动它:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
在onCreate()
之上,像这样:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random random = new Random(100);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
tv = (TextView) findViewById(R.id.textView);
tv.setText("change");
}
});
}
答案 1 :(得分:0)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Random random = new Random(100);
// View rootView = inflater.inflate(R.layout.fragment_main, container, false);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
tv = (TextView) findViewById(R.id.textView);
tv.setText("change");
}
}); }