Android:静态选项菜单给出空指针
我正在尝试做与这里的问题相同的事情。
但是,遵循第一个答案的第二个选项给了我一个空指针异常。基本上是行不通的。我缺少什么。下面是我的代码文件。
预先非常感谢您。
AppSetup.java
package com.connectionmanager.app;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AppSetup extends Activity implements OnClickListener
{
private AcmMenu acmMenu;
private Context appContext;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.app_setup);
appContext = this.getApplicationContext();
Log.v("Hello","Bringing up application setup Dialog box");
//Set up buttons
Button btnSave = (Button) findViewById(R.id.ButtonSave);
Button btnContinue = (Button) findViewById(R.id.ButtonContinue);
btnSave.setOnClickListener(this);
btnContinue.setOnClickListener(this);
this.resetLabels();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
this.finish();
Log.v("Hello","Back key pressed");
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
acmMenu = new AcmMenu(menu,this.getApplicationContext());
acmMenu.inflate();
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
acmMenu.onSelectedItem(item,appContext );
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0.getId() == (R.id.ButtonSave))
{
// Save information and then finish.
AppDatabase db = new AppDatabase(this.getApplicationContext());
db.setHostname( ((EditText)findViewById(R.id.editText_hostname)).getText().toString());
db.setRegistrationUrl(((EditText)findViewById(R.id.editText_registration)).getText().toString());
db.setSsid(((EditText)findViewById(R.id.editText_ssid)).getText().toString());
db.closeDb();
this.resetLabels();
}
if(arg0.getId() == (R.id.ButtonContinue))
{
this.finish();
}
}
private void resetLabels()
{
//Get and set existing values from the database.
AppDatabase db = new AppDatabase(this.getApplicationContext());
Log.v("Hello","Hostname from app:" +db.getHostname() );
TextView hostname_current = (TextView)findViewById(R.id.lbl_hostname_current);
hostname_current.setText("(Current: "+db.getHostname()+")");
((EditText)findViewById(R.id.editText_hostname)).setText(db.getHostname());
TextView registration_current = (TextView)findViewById(R.id.lbl_registration_current);
registration_current.setText("(Current: "+db.getRegistrationUrl()+")");
((EditText)findViewById(R.id.editText_registration)).setText(db.getRegistrationUrl());
TextView chat_current = (TextView)findViewById(R.id.lbl_chat_current);
//chat_current.setText("(Current: "+db.getRegistrationUrl()+")");
//((EditText)findViewById(R.id.editText_chat)).setText(db.getRegistrationUrl());
TextView ssid_current = (TextView)findViewById(R.id.lbl_ssid_current);
ssid_current.setText("(Current: "+db.getSsid()+")");
((EditText)findViewById(R.id.editText_ssid)).setText(db.getSsid());
db.closeDb();
}
}
AcmMenu.java
package com.connectionmanager.app;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class AcmMenu extends Activity{
private Menu acmMenu;
private static Context classContext;
public AcmMenu(Menu menu, Context cont)
{
classContext = cont;
acmMenu = menu;
}
public void inflate()
{
if (classContext == null)
{
Log.v("Hello","Context null");
}
else
{
MenuInflater inflater = new MenuInflater(classContext);
inflater.inflate(R.menu.acm_menu, acmMenu);
}
}
public void onSelectedItem(MenuItem item, Context con)
{
Log.v("Hello","Starting activity");
Intent gotoHelpScreen = new Intent(con, Help.class);
try
{
startActivity(new Intent(con, NewUserAccount.class));
}
catch (Exception e)
{
e.printStackTrace();
Log.v("Hello","Exception: " + e.toString());
}
/*/ Handle item selection
switch (item.getItemId())
{
case R.id.menu_wifi:
toggleWifi();
return true;
case R.id.menu_account:
gotoAccounts();
return true;
case R.id.menu_help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}*/
}
private void toggleWifi()
{
}
private void gotoAccounts()
{
}
private void showHelp()
{
}
}
Help.java
package com.connectionmanager.app;
import android.app.Activity;
import android.os.Bundle;
public class Help extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.help);
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.connectionmanager.app"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<activity android:name=".CMapp"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NewUserAccount" />
<activity android:name=".FoundSavedUser" />
<activity android:name=".AppSetup" />
<activity android:name=".Help" />
</application>
</manifest>
Stacktrace
05-27 12:07:03.930: WARN/Server(7134): Addition of the standard header "cache-control" is not allowed. Please use the equivalent property in the Restlet API.
05-27 12:07:03.930: WARN/Server(7134): Addition of the standard header "expires" is not allowed. Please use the equivalent property in the Restlet API.
05-27 12:07:03.961: WARN/System.err(8135): java.lang.NullPointerException
05-27 12:07:03.961: WARN/System.err(8135): at android.app.Activity.startActivityForResult(Activity.java:2890)
05-27 12:07:03.961: WARN/System.err(8135): at android.app.Activity.startActivity(Activity.java:2996)
05-27 12:07:03.961: WARN/System.err(8135): at com.connectionmanager.app.AcmMenu.onSelectedItem(AcmMenu.java:43)
05-27 12:07:03.961: WARN/System.err(8135): at com.connectionmanager.app.AppSetup.onOptionsItemSelected(AppSetup.java:61)
05-27 12:07:03.961: WARN/System.err(8135): at android.app.Activity.onMenuItemSelected(Activity.java:2234)
05-27 12:07:03.970: WARN/System.err(8135): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:747)
05-27 12:07:03.970: WARN/System.err(8135): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:160)
05-27 12:07:03.970: WARN/System.err(8135): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:886)
05-27 12:07:03.970: WARN/System.err(8135): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:545)
05-27 12:07:03.970: WARN/System.err(8135): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
05-27 12:07:03.970: WARN/System.err(8135): at android.view.View$PerformClick.run(View.java:8867)
05-27 12:07:03.970: WARN/System.err(8135): at android.os.Handler.handleCallback(Handler.java:587)
05-27 12:07:03.970: WARN/System.err(8135): at android.os.Handler.dispatchMessage(Handler.java:92)
05-27 12:07:03.970: WARN/System.err(8135): at android.os.Looper.loop(Looper.java:143)
05-27 12:07:03.970: WARN/System.err(8135): at android.app.ActivityThread.main(ActivityThread.java:5068)
05-27 12:07:03.970: WARN/System.err(8135): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 12:07:03.970: WARN/System.err(8135): at java.lang.reflect.Method.invoke(Method.java:521)
05-27 12:07:03.970: WARN/System.err(8135): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-27 12:07:03.970: WARN/System.err(8135): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-27 12:07:03.970: WARN/System.err(8135): at dalvik.system.NativeStart.main(Native Method)
05-27 12:07:03.980: VERBOSE/WindowManager(1292): Remove Window{4757e618 AtchDlg:com.connectionmanager.app/com.connectionmanager.app.AppSetup paused=false}: mSurface=Surface(name=AtchDlg:com.connectionmanager.app/com.connectionmanager.app.AppSetup, identity=1792) mExiting=false isAnimating=false app-animation=null inPendingTransaction=false mDisplayFrozen=false
05-27 12:07:03.990: ERROR/WindowManager(1292): return in removeWindowLocked
05-27 12:07:04.000: DEBUG/InputManagerService(1292): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@475c3990
I am trying to do the same as the question in here.
However, following the 2nd option of the 1st answer gives me a null pointer exception. Basically it does not work. What am I missing. Below are my code files.
Thank you very much in advance.
AppSetup.java
package com.connectionmanager.app;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AppSetup extends Activity implements OnClickListener
{
private AcmMenu acmMenu;
private Context appContext;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.app_setup);
appContext = this.getApplicationContext();
Log.v("Hello","Bringing up application setup Dialog box");
//Set up buttons
Button btnSave = (Button) findViewById(R.id.ButtonSave);
Button btnContinue = (Button) findViewById(R.id.ButtonContinue);
btnSave.setOnClickListener(this);
btnContinue.setOnClickListener(this);
this.resetLabels();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
this.finish();
Log.v("Hello","Back key pressed");
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
acmMenu = new AcmMenu(menu,this.getApplicationContext());
acmMenu.inflate();
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
acmMenu.onSelectedItem(item,appContext );
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0.getId() == (R.id.ButtonSave))
{
// Save information and then finish.
AppDatabase db = new AppDatabase(this.getApplicationContext());
db.setHostname( ((EditText)findViewById(R.id.editText_hostname)).getText().toString());
db.setRegistrationUrl(((EditText)findViewById(R.id.editText_registration)).getText().toString());
db.setSsid(((EditText)findViewById(R.id.editText_ssid)).getText().toString());
db.closeDb();
this.resetLabels();
}
if(arg0.getId() == (R.id.ButtonContinue))
{
this.finish();
}
}
private void resetLabels()
{
//Get and set existing values from the database.
AppDatabase db = new AppDatabase(this.getApplicationContext());
Log.v("Hello","Hostname from app:" +db.getHostname() );
TextView hostname_current = (TextView)findViewById(R.id.lbl_hostname_current);
hostname_current.setText("(Current: "+db.getHostname()+")");
((EditText)findViewById(R.id.editText_hostname)).setText(db.getHostname());
TextView registration_current = (TextView)findViewById(R.id.lbl_registration_current);
registration_current.setText("(Current: "+db.getRegistrationUrl()+")");
((EditText)findViewById(R.id.editText_registration)).setText(db.getRegistrationUrl());
TextView chat_current = (TextView)findViewById(R.id.lbl_chat_current);
//chat_current.setText("(Current: "+db.getRegistrationUrl()+")");
//((EditText)findViewById(R.id.editText_chat)).setText(db.getRegistrationUrl());
TextView ssid_current = (TextView)findViewById(R.id.lbl_ssid_current);
ssid_current.setText("(Current: "+db.getSsid()+")");
((EditText)findViewById(R.id.editText_ssid)).setText(db.getSsid());
db.closeDb();
}
}
AcmMenu.java
package com.connectionmanager.app;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class AcmMenu extends Activity{
private Menu acmMenu;
private static Context classContext;
public AcmMenu(Menu menu, Context cont)
{
classContext = cont;
acmMenu = menu;
}
public void inflate()
{
if (classContext == null)
{
Log.v("Hello","Context null");
}
else
{
MenuInflater inflater = new MenuInflater(classContext);
inflater.inflate(R.menu.acm_menu, acmMenu);
}
}
public void onSelectedItem(MenuItem item, Context con)
{
Log.v("Hello","Starting activity");
Intent gotoHelpScreen = new Intent(con, Help.class);
try
{
startActivity(new Intent(con, NewUserAccount.class));
}
catch (Exception e)
{
e.printStackTrace();
Log.v("Hello","Exception: " + e.toString());
}
/*/ Handle item selection
switch (item.getItemId())
{
case R.id.menu_wifi:
toggleWifi();
return true;
case R.id.menu_account:
gotoAccounts();
return true;
case R.id.menu_help:
showHelp();
return true;
default:
return super.onOptionsItemSelected(item);
}*/
}
private void toggleWifi()
{
}
private void gotoAccounts()
{
}
private void showHelp()
{
}
}
Help.java
package com.connectionmanager.app;
import android.app.Activity;
import android.os.Bundle;
public class Help extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.help);
}
}
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.connectionmanager.app"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
<activity android:name=".CMapp"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NewUserAccount" />
<activity android:name=".FoundSavedUser" />
<activity android:name=".AppSetup" />
<activity android:name=".Help" />
</application>
</manifest>
Stacktrace
05-27 12:07:03.930: WARN/Server(7134): Addition of the standard header "cache-control" is not allowed. Please use the equivalent property in the Restlet API.
05-27 12:07:03.930: WARN/Server(7134): Addition of the standard header "expires" is not allowed. Please use the equivalent property in the Restlet API.
05-27 12:07:03.961: WARN/System.err(8135): java.lang.NullPointerException
05-27 12:07:03.961: WARN/System.err(8135): at android.app.Activity.startActivityForResult(Activity.java:2890)
05-27 12:07:03.961: WARN/System.err(8135): at android.app.Activity.startActivity(Activity.java:2996)
05-27 12:07:03.961: WARN/System.err(8135): at com.connectionmanager.app.AcmMenu.onSelectedItem(AcmMenu.java:43)
05-27 12:07:03.961: WARN/System.err(8135): at com.connectionmanager.app.AppSetup.onOptionsItemSelected(AppSetup.java:61)
05-27 12:07:03.961: WARN/System.err(8135): at android.app.Activity.onMenuItemSelected(Activity.java:2234)
05-27 12:07:03.970: WARN/System.err(8135): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:747)
05-27 12:07:03.970: WARN/System.err(8135): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:160)
05-27 12:07:03.970: WARN/System.err(8135): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:886)
05-27 12:07:03.970: WARN/System.err(8135): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:545)
05-27 12:07:03.970: WARN/System.err(8135): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
05-27 12:07:03.970: WARN/System.err(8135): at android.view.View$PerformClick.run(View.java:8867)
05-27 12:07:03.970: WARN/System.err(8135): at android.os.Handler.handleCallback(Handler.java:587)
05-27 12:07:03.970: WARN/System.err(8135): at android.os.Handler.dispatchMessage(Handler.java:92)
05-27 12:07:03.970: WARN/System.err(8135): at android.os.Looper.loop(Looper.java:143)
05-27 12:07:03.970: WARN/System.err(8135): at android.app.ActivityThread.main(ActivityThread.java:5068)
05-27 12:07:03.970: WARN/System.err(8135): at java.lang.reflect.Method.invokeNative(Native Method)
05-27 12:07:03.970: WARN/System.err(8135): at java.lang.reflect.Method.invoke(Method.java:521)
05-27 12:07:03.970: WARN/System.err(8135): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-27 12:07:03.970: WARN/System.err(8135): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-27 12:07:03.970: WARN/System.err(8135): at dalvik.system.NativeStart.main(Native Method)
05-27 12:07:03.980: VERBOSE/WindowManager(1292): Remove Window{4757e618 AtchDlg:com.connectionmanager.app/com.connectionmanager.app.AppSetup paused=false}: mSurface=Surface(name=AtchDlg:com.connectionmanager.app/com.connectionmanager.app.AppSetup, identity=1792) mExiting=false isAnimating=false app-animation=null inPendingTransaction=false mDisplayFrozen=false
05-27 12:07:03.990: ERROR/WindowManager(1292): return in removeWindowLocked
05-27 12:07:04.000: DEBUG/InputManagerService(1292): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@475c3990
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这样的事情:
Try something like this :