在开始时说出来 - 我不是一个优秀的程序员。
我试图用删除和编辑功能创建一个上下文菜单,但我有点编辑功能有问题。一旦用户按下上下文菜单中的编辑按钮,他就应该能够更改长按按钮的名称以打开上下文菜单。
@Override
public boolean onContextItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.Bearbeiten:
EditText editText = (EditText) findViewById(R.id.editText);
buttonActOne.setText(editText.getText());
return true;
当然,一旦用户按下编辑按钮,程序就会一直崩溃,并且我有点想法我是怎么做到的。
我真的希望有人能给我一个很好的答案。
编辑:
好的,首先要感谢答案。我发布了我的完整MainActivity.java
package com.example.michl.myapplication5;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textViewIntro;
public Button buttonActOne;
public Button buttonActTwo;
public Button buttonActThree;
public Button buttonActFour;
public Button buttonActFive;
public Button buttonActSix;
public Button buttonActSeven;
public Button buttonActEight;
public Button buttonActNine;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewIntro = (TextView) findViewById(R.id.textViewIntro);
Button ButtonActOne = (Button) findViewById(R.id.buttonActOne);
ButtonActOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Seite_zwei.class));
Button ButtonActOne = (Button)findViewById(R.id.buttonActOne);
registerForContextMenu(ButtonActOne);
}
});
Button ButtonActTwo = (Button) findViewById(R.id.buttonActTwo);
ButtonActTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Seite_drei.class));
Button ButtonActTwo = (Button)findViewById(R.id.buttonActTwo);
registerForContextMenu(ButtonActTwo);
}
});
Button ButtonActThree = (Button) findViewById(R.id.buttonActThree);
ButtonActThree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Seite_vier.class));
Button ButtonActThree = (Button)findViewById(R.id.buttonActThree);
registerForContextMenu(ButtonActThree);
}
});
Button ButtonActFour = (Button) findViewById(R.id.buttonActFour);
ButtonActFour.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Seite_fuenf.class));
Button ButtonActFour = (Button)findViewById(R.id.buttonActFour);
registerForContextMenu(ButtonActFour);
}
});
Button ButtonActFive = (Button) findViewById(R.id.buttonActFive);
ButtonActFive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Seite_sechs.class));
Button ButtonActFive = (Button)findViewById(R.id.buttonActFive);
registerForContextMenu(ButtonActFive);
}
});
Button ButtonActSix = (Button) findViewById(R.id.buttonActSix);
ButtonActSix.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Seite_sieben.class));
Button ButtonActSix = (Button)findViewById(R.id.buttonActSix);
registerForContextMenu(ButtonActSix);
}
});
Button ButtonActSeven = (Button) findViewById(R.id.buttonActSeven);
ButtonActSeven.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Seite_acht.class));
Button ButtonActSeven = (Button)findViewById(R.id.buttonActSeven);
registerForContextMenu(ButtonActSeven);
}
});
Button ButtonActEight = (Button) findViewById(R.id.buttonActEight);
ButtonActEight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Seite_neun.class));
Button ButtonActEight = (Button)findViewById(R.id.buttonActEight);
registerForContextMenu(ButtonActEight);
}
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.menu_main, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.Bearbeiten:
EditText editText = (EditText) findViewById(R.id.editText);
buttonActOne.setText(editText.getText());
return true;
case R.id.Löschen:
return true;
default:
}
return super.onContextItemSelected(item);
}
@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);
}
}
当我启动程序并尝试编辑某些内容时,这是我的logcat日志。
08-28 13:52:06.135 26482-26482/com.example.michl.myapplication5 D/dalvikvm﹕ Late-enabling CheckJNI
08-28 13:52:06.150 26482-26488/com.example.michl.myapplication5 E/jdwp﹕ Failed sending reply to debugger: Broken pipe
08-28 13:52:06.150 26482-26488/com.example.michl.myapplication5 D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
08-28 13:52:06.290 26482-26482/com.example.michl.myapplication5 D/libEGL﹕ loaded /system/lib/egl/libEGL_mali.so
08-28 13:52:06.295 26482-26482/com.example.michl.myapplication5 D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_mali.so
08-28 13:52:06.295 26482-26482/com.example.michl.myapplication5 D/libEGL﹕ loaded /system/lib/egl/libGLESv2_mali.so
08-28 13:52:06.300 26482-26482/com.example.michl.myapplication5 D/﹕ Device driver API match
Device driver API version: 10
User space API version: 10
08-28 13:52:06.300 26482-26482/com.example.michl.myapplication5 D/﹕ mali: REVISION=Linux-r2p4-02rel0 BUILD_DATE=Thu Oct 25 08:43:05 KST 2012
08-28 13:52:06.315 26482-26482/com.example.michl.myapplication5 D/OpenGLRenderer﹕ Enabling debug mode 0
08-28 13:52:17.355 26482-26482/com.example.michl.myapplication5 I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
08-28 13:52:17.355 26482-26482/com.example.michl.myapplication5 W/dalvikvm﹕ VFY: unable to resolve virtual method 13334: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
08-28 13:52:17.355 26482-26482/com.example.michl.myapplication5 D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
08-28 13:52:17.360 26482-26482/com.example.michl.myapplication5 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
08-28 13:52:17.360 26482-26485/com.example.michl.myapplication5 D/dalvikvm﹕ GC_CONCURRENT freed 188K, 14% free 9612K/11143K, paused 12ms+18ms, total 49ms
08-28 13:52:17.360 26482-26482/com.example.michl.myapplication5 W/dalvikvm﹕ VFY: unable to resolve virtual method 412: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
08-28 13:52:17.360 26482-26482/com.example.michl.myapplication5 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
08-28 13:52:17.360 26482-26482/com.example.michl.myapplication5 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
08-28 13:52:17.360 26482-26482/com.example.michl.myapplication5 W/dalvikvm﹕ VFY: unable to resolve virtual method 434: Landroid/content/res/TypedArray;.getType (I)I
08-28 13:52:17.360 26482-26482/com.example.michl.myapplication5 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
08-28 13:52:17.465 26482-26482/com.example.michl.myapplication5 E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-28 13:52:17.465 26482-26482/com.example.michl.myapplication5 E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-28 13:52:18.345 26482-26482/com.example.michl.myapplication5 E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-28 13:52:18.345 26482-26482/com.example.michl.myapplication5 E/SpannableStringBuilder﹕ SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
08-28 13:52:19.605 26482-26482/com.example.michl.myapplication5 D/AbsListView﹕ Get MotionRecognitionManager
08-28 13:52:19.635 26482-26482/com.example.michl.myapplication5 W/ResourceType﹕ Failure getting entry for 0x010802c1 (t=7 e=705) in package 0 (error -75)
08-28 13:52:20.935 26482-26482/com.example.michl.myapplication5 D/AndroidRuntime﹕ Shutting down VM
08-28 13:52:20.935 26482-26482/com.example.michl.myapplication5 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4191e2a0)
08-28 13:52:20.955 26482-26482/com.example.michl.myapplication5 E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.michl.myapplication5.MainActivity.onContextItemSelected(MainActivity.java:161)
at android.app.Activity.onMenuItemSelected(Activity.java:2647)
at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:3921)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:193)
at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:934)
at android.widget.AdapterView.performItemClick(AdapterView.java:301)
at android.widget.AbsListView.performItemClick(AbsListView.java:1280)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3071)
at android.widget.AbsListView$1.run(AbsListView.java:3973)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
08-28 13:52:31.590 26482-26482/com.examp
le.michl.myapplication5 I/Process﹕ Sending signal. PID: 26482 SIG: 9
答案 0 :(得分:0)
在Activity的onCreate方法上声明你的按钮!
Button buttonActOne = findViewById(R.id.buttonActOneId);
然后将侦听器添加到它:
buttonActOne.setOnClickListener(this);
setOnClickListener实现了View.OnClickListener接口,所以@override方法必须是onClick(View v),然后在交换机中你可以使用v.getId();
简历中:
public class YourActivityName extends Activity implements View.OnClickListener {
private Button buttonActOne;
@Override
public void onCreate(Bundle b) {
//set your layout
buttonActOne = findViewById(R.id.buttonActOneId); //put the id defined in your xml layout
}
@Override
public void onClick(View view) {
if (View.getId(R.id.Bearbeiten)) {
EditText editText = (EditText) findViewById(R.id.editText);
buttonActOne.setText(editText.getText());
}
}
}