-1

我有一个简单的应用程序,在主选项卡上,我有一个按钮。单击按钮时,假设启动另一个选项卡。但是,当我单击按钮时,屏幕变为空白,并显示:

不幸的是 appname 已停止

我哪里做错了?

代码片段包括:

  1. MainActivity.Java

       public class MainActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            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.main, menu);
            return true;
    }
    
    public void serveMenu(View view) {
      // Do something in response to button
    Intent intent = new Intent(this, ServeMenuActivity.class);
    startActivity(intent);
    
      }
        }
    
  2. ServeMenuActivity.Java

     import android.app.Activity;
     import android.content.Intent;
     import android.os.Bundle;
     import android.view.Menu;
    
       public class ServeMenuActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_serve_menu);
    Intent intent = getIntent();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.serve_menu, menu);
    return true;
     }
    
       }
    
  3. AdriodManifest.xml

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.mywinsystems.tkrctennistips.ServeMenuActivity"
        android:label="@string/title_activity_serve_menu" >
    
    </activity>
    

4

2 回答 2

0

检查是否在 main.xml 按钮上添加了 onClick serveMenu,即android:onClick="serveMenu"

于 2013-10-06T05:27:24.280 回答
0
10-06 17:30:41.249: D/OpenGLRenderer(18009): Enabling debug mode 0 10-06 17:31:46.774: D/AndroidRuntime(18009): Shutting down VM 10-06 17:31:46.774: W/dalvikvm(18009): threadid=1: thread exiting with uncaught exception (group=0x4108f2a0) 10-06 17:31:46.784: E/AndroidRuntime(18009): FATAL EXCEPTION: main 10-06 17:31:46.784: E/AndroidRuntime(18009): java.lang.IllegalStateException: Could not find a method ServeMenu(View) in the activity class com.mywinsystems.tkrctennistips.MainActivity for onClick handler on view class android.widget.Button with id 'button1' 

根据您上面的 locgcate 尝试重命名

public void serveMenu(View view) {
  // Do something in response to button
Intent intent = new Intent(this, ServeMenuActivity.class);
startActivity(intent);

  }

public void ServeMenu(View view) {
  // Do something in response to button
Intent intent = new Intent(this, ServeMenuActivity.class);
startActivity(intent);

  }
于 2013-10-06T06:07:37.460 回答