XML 更改布局按钮崩溃的应用程序
XML change Layout button crashing app
我制作了一个简单的应用程序,因为我刚开始使用 android。我制作了一个按钮来更改布局,但在测试后每次都会使应用程序崩溃。
这是我的代码
package nathanschmidt.nathan;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.graphics.Color;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends ActionBarActivity {
TextView numberView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberView = (TextView) findViewById(R.id.numberview);
NumberPicker numberPicker = (NumberPicker) findViewById(R.id.numberPicker);
numberPicker.setMaxValue(10);
numberPicker.setMinValue(0);
numberPicker.setWrapSelectorWheel(true);
numberPicker.setOnValueChangedListener(
new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
int color;
if (newVal < 1) {
color = Color.parseColor("#000000");
} else if (newVal < 2) {
color = Color.parseColor("#01093B");
} else if (newVal < 3) {
color = Color.parseColor("#000C57");
} else if (newVal < 4) {
color = Color.parseColor("#000F73");
} else if (newVal < 5) {
color = Color.parseColor("#00128a");
} else if (newVal < 6) {
color = Color.parseColor("#00159E");
} else if (newVal < 7) {
color = Color.parseColor("#0017B0");
} else if (newVal < 8) {
color = Color.parseColor("#001AC4");
} else if (newVal < 9) {
color = Color.parseColor("#001FE8");
} else {
color = Color.parseColor("#0022FF");
}
numberView.setTextColor(color);
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
// Watch for button clicks.
Button button = (Button)findViewById(R.id.change);
button.setOnClickListener(submitListener);
}
private OnClickListener submitListener = new OnClickListener() {
public void onClick(View v) {
}
};
xml
<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"
android:background="@drawable/os2">
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/numberview"
android:textSize="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/numberPicker"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="change"
android:id="@+id/change"
android:layout_below="@+id/numberPicker"
android:layout_centerHorizontal="true" />
真正崩溃的是那两行代码
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
你在这里两次调用 Activity
class onCreate()
方法,这根本不是一个好的做法,你正在更改 setContentView()
方法中的布局,然后当您使用从 activity_main 布局初始化的 numberView 时,您当前的布局是 about 布局。我想知道你得到的异常是 NullPointerException
还是 IllegalStateException
.
我制作了一个简单的应用程序,因为我刚开始使用 android。我制作了一个按钮来更改布局,但在测试后每次都会使应用程序崩溃。 这是我的代码
package nathanschmidt.nathan;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.graphics.Color;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends ActionBarActivity {
TextView numberView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numberView = (TextView) findViewById(R.id.numberview);
NumberPicker numberPicker = (NumberPicker) findViewById(R.id.numberPicker);
numberPicker.setMaxValue(10);
numberPicker.setMinValue(0);
numberPicker.setWrapSelectorWheel(true);
numberPicker.setOnValueChangedListener(
new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
int color;
if (newVal < 1) {
color = Color.parseColor("#000000");
} else if (newVal < 2) {
color = Color.parseColor("#01093B");
} else if (newVal < 3) {
color = Color.parseColor("#000C57");
} else if (newVal < 4) {
color = Color.parseColor("#000F73");
} else if (newVal < 5) {
color = Color.parseColor("#00128a");
} else if (newVal < 6) {
color = Color.parseColor("#00159E");
} else if (newVal < 7) {
color = Color.parseColor("#0017B0");
} else if (newVal < 8) {
color = Color.parseColor("#001AC4");
} else if (newVal < 9) {
color = Color.parseColor("#001FE8");
} else {
color = Color.parseColor("#0022FF");
}
numberView.setTextColor(color);
}
});
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
// Watch for button clicks.
Button button = (Button)findViewById(R.id.change);
button.setOnClickListener(submitListener);
}
private OnClickListener submitListener = new OnClickListener() {
public void onClick(View v) {
}
};
xml
<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"
android:background="@drawable/os2">
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/numberview"
android:textSize="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/numberPicker"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="change"
android:id="@+id/change"
android:layout_below="@+id/numberPicker"
android:layout_centerHorizontal="true" />
真正崩溃的是那两行代码
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
你在这里两次调用 Activity
class onCreate()
方法,这根本不是一个好的做法,你正在更改 setContentView()
方法中的布局,然后当您使用从 activity_main 布局初始化的 numberView 时,您当前的布局是 about 布局。我想知道你得到的异常是 NullPointerException
还是 IllegalStateException
.