gpt4 book ai didi

Android 应用程序在实现数字选择器对话框后停止工作

转载 作者:行者123 更新时间:2023-11-29 15:49:05 27 4
gpt4 key购买 nike

我正在尝试将 NumberPicker 作为对话框添加到通过按下按钮打开的 Activity 中。现在,当我尝试使用 NumberPicker 对话框从一个 Activity 移动到另一个 Activity 时,应用程序停止工作。我不明白调试错误试图告诉我什么,有帮助吗?

NumberPicker 对话框之前的页面

public class RoomDescription extends ActionBarActivity {

public AlertDialog.Builder dialogBuilder;
public List<String> myFixtures = new ArrayList<String>();
public String listItem;

private void setFixtureName()
{
dialogBuilder = new AlertDialog.Builder(this);
final EditText txtInput = new EditText(this);


dialogBuilder.setTitle("New Fixture");
dialogBuilder.setMessage("What is the fixture name?");
dialogBuilder.setView(txtInput);
dialogBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
myFixtures.add(txtInput.getText().toString());
populateListView();
Toast.makeText(getApplicationContext(), "Fixture has been added.", Toast.LENGTH_SHORT).show();
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{

}
});

AlertDialog dialogFixtureName = dialogBuilder.create();
dialogFixtureName.show();
}

private void removeFixture()
{
dialogBuilder = new AlertDialog.Builder(this);
final EditText txtInput = new EditText(this);

dialogBuilder.setTitle("Select Fixture to Remove");
dialogBuilder.setSingleChoiceItems(myFixtures.toArray(new String[myFixtures.size()]), -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
myFixtures.remove(which);
populateListView();
Toast.makeText(getApplicationContext(), "Fixture has been removed.", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});

AlertDialog dialogFixtureName = dialogBuilder.create();
dialogFixtureName.show();
}
private void populateListView()
{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.items, myFixtures);

ListView list = (ListView) findViewById(R.id.FixtureList);
list.setAdapter(adapter);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_room_description);

populateListView();

ListView list = (ListView) findViewById(R.id.FixtureList);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
default:
Intent i = new Intent(RoomDescription.this, FixtureDescription.class);
TextView textItem = (TextView) view;
String textString = textItem.getText().toString();
i.putExtra("textString",textString);
startActivity(i);
break;
}

}

});
}

@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_room_description, 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;
}
switch (item.getItemId())
{
case R.id.action_settings:
return true;
case R.id.newFixture:
setFixtureName();
break;
case R.id.removeFixture:
removeFixture();
}


return super.onOptionsItemSelected(item);
}
}

NumberPicker 对话框页面

public class FixtureDescription extends ActionBarActivity {

public AlertDialog.Builder dialogBuilder;
public int btnFixtureString = 0;
public Button btnFixture = (Button) findViewById(R.id.stepper);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(activity_fixture_description);
TextView textFixture = (TextView) findViewById(R.id.FixtureDescName);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String s = extras.getString("textString");
textFixture.setText(s);
}
btnFixture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
stepperDialog();
}
});
}



private void stepperDialog() {
final NumberPicker numberPicker;
Button btnAdd;
Button btnCancel;
final Dialog customDialog = new Dialog(this);


customDialog.setContentView(R.layout.numberpicker);
customDialog.setTitle("Number of Fixtures");
numberPicker = (NumberPicker) customDialog.findViewById(R.id.numberPicker);
btnAdd = (Button) customDialog.findViewById(R.id.buttonAdd);
btnCancel = (Button) customDialog.findViewById(R.id.buttonCancel);

numberPicker.setMaxValue(100);
numberPicker.setMinValue(0);
btnAdd.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
btnFixtureString = numberPicker.getValue();
Display();
customDialog.dismiss();
}
});
btnCancel.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
customDialog.dismiss();
}
});
customDialog.show();
}

public void Display()
{
btnFixture.setText(btnFixtureString);
}
@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_fixture_description, 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);
}

}

调试错误报告

07-15 13:59:27.065  11951-11951/com.customledsupply.ledaudit E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.customledsupply.ledaudit, PID: 11951
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.customledsupply.ledaudit/com.customledsupply.ledaudit.FixtureDescription}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
at android.app.Activity.findViewById(Activity.java:2072)
at com.customledsupply.ledaudit.FixtureDescription.<init>(FixtureDescription.java:21)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1606)
at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
            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)

最佳答案

不要像这样初始化这个变量:

public Button btnFixture = (Button) findViewById(R.id.stepper);

您应该在 onCreate 中通过 findViewById 初始化 btnFixture

关于Android 应用程序在实现数字选择器对话框后停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31439015/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com