【问题标题】:8 button intent crash8 按钮意图崩溃
【发布时间】:2015-11-01 02:19:11
【问题描述】:

好的,感谢阅读。 我有一个用于 8 意图的 8 按钮。

在按钮 1 意图中。正确地工作。并开另一堂课。 但是当我尝试单击按钮 2 - 8 然后崩溃时。

我已经添加了 8 个类来显示,但仍然崩溃。

我的问题是。如何实现超过 1 个意图按钮?

这是我的主要代码

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
Button button1;
Button button02;
Button button3;
Button button4;
Button button5;
Button button6;
Button button7;
Button button8;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.activity_main);

    // Locate the button in activity_main.xml
    button1 = (Button) findViewById(R.id.button1);
    button02 = (Button) findViewById(R.id.button02);
    button3 = (Button) findViewById(R.id.button3);
    button4 = (Button) findViewById(R.id.button4);
    button5 = (Button) findViewById(R.id.button5);
    button6 = (Button) findViewById(R.id.button6);
    button7 = (Button) findViewById(R.id.button7);
    button8 = (Button) findViewById(R.id.button8);

    // Capture button clicks
    button1.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

            // Start NewActivity.class
            Intent myIntent = new Intent(MainActivity.this,
                    Bbarung.class);
            startActivity(myIntent);
        }
    });
    button02.setOnClickListener(new OnClickListener() {
        public void onClick(View arg1) {

            // Start NewActivity.class
            Intent myIntent = new Intent(MainActivity.this,
                    Bpanerus.class);
            startActivity(myIntent);


        }
    });
    button3.setOnClickListener(new OnClickListener() {
        public void onClick(View arg2) {

            // Start NewActivity.class
            Intent myIntent = new Intent(MainActivity.this,
                    Gong.class);
            startActivity(myIntent);
        }
    });
    button4.setOnClickListener(new OnClickListener() {
        public void onClick(View arg3) {

            // Start NewActivity.class
            Intent myIntent = new Intent(MainActivity.this,
                    Kenong.class);
            startActivity(myIntent);
        }
    });
    button5.setOnClickListener(new OnClickListener() {
        public void onClick(View arg4) {

            // Start NewActivity.class
            Intent myIntent = new Intent(MainActivity.this,
                    Sbarung.class);
            startActivity(myIntent);
        }
    });
    button6.setOnClickListener(new OnClickListener() {
        public void onClick(View arg5) {

            // Start NewActivity.class
            Intent myIntent = new Intent(MainActivity.this,
                    Sdemung.class);
            startActivity(myIntent);
        }
    });
    button7.setOnClickListener(new OnClickListener() {
        public void onClick(View arg6) {

            // Start NewActivity.class
            Intent myIntent = new Intent(MainActivity.this,
                    Spanerus.class);
            startActivity(myIntent);
        }
    });
    button8.setOnClickListener(new OnClickListener() {
        public void onClick(View arg7) {

            // Start NewActivity.class
            Intent myIntent = new Intent(MainActivity.this,
                    Slenthem.class);
            startActivity(myIntent);
        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

这是我的 xml 代码。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<LinearLayout

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
       android:id="@+id/button1"
       android:layout_width="1280px"
       android:layout_height="309px"
       android:background="@drawable/buttonbbarung" />

    <Button
       android:id="@+id/button02"
       android:layout_width="1280px"
       android:layout_height="309px"
       android:background="@drawable/buttonbpanerus" />

    <Button
       android:id="@+id/button3"
       android:layout_width="1280px"
       android:layout_height="309px"
       android:background="@drawable/buttongong" />

    <Button
        android:id="@+id/button4"
       android:layout_width="1280px"
       android:layout_height="309px"
       android:background="@drawable/buttonkenong" />

    <Button
       android:id="@+id/button5"
       android:layout_width="1280px"
       android:layout_height="309px"
       android:background="@drawable/buttonsbarung" />


    <Button
        android:id="@+id/button6"
       android:layout_width="1280px"
       android:layout_height="309px"
       android:background="@drawable/buttonsdemung" />



    <Button
         android:id="@+id/button7"
       android:layout_width="1280px"
       android:layout_height="309px"
       android:background="@drawable/buttonspanerus" />



    <Button
         android:id="@+id/button8"
       android:layout_width="1280px"
       android:layout_height="309px"
       android:background="@drawable/buttonslenthem" />


</LinearLayout>


</ScrollView>

我的清单

<uses-sdk
    android:minSdkVersion="17"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" 
        android:screenOrientation="landscape" >>
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.main.Bbarung"
        android:label="@string/app_name"
        android:screenOrientation="landscape">
    </activity>
    <activity
        android:name="com.example.main.Bpanerus"
        android:label="@string/hello_world"
        android:screenOrientation="landscape">
    </activity>
    <activity
        android:name="com.example.main.Gong"
        android:label="@string/app_name"
        android:screenOrientation="landscape">
    </activity>
    <activity
        android:name="com.example.main.Kenong"
        android:label="@string/app_name"
        android:screenOrientation="landscape">
    </activity>
    <activity
        android:name="com.example.main.Sbarung"
        android:label="@string/app_name"
        android:screenOrientation="landscape">
    </activity>
    <activity
        android:name="com.example.main.Sdemung"
        android:label="@string/app_name"
        android:screenOrientation="landscape">
    </activity>
    <activity
        android:name="com.example.main.Spanerus"
        android:label="@string/app_name"
        android:screenOrientation="landscape">
    </activity>
    <activity
        android:name="com.example.main.Slenthem"
        android:label="@string/app_name"
        android:screenOrientation="landscape">
    </activity>
  </application>

  </manifest>

最后是我的 logcat 错误

08-08 19:34:45.550: E/AndroidRuntime(1172): FATAL EXCEPTION: main
08-08 19:34:45.550: E/AndroidRuntime(1172): java.lang.RuntimeException:       
Unable to start activity   
ComponentInfo{com.example.main/com.example.main.MainActivity}: 
java.lang.ClassCastException: com.example.main.MainActivity cannot be cast   
to android.view.View$OnClickListener
08-08 19:34:45.550: E/AndroidRuntime(1172):     at  
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-08 19:34:45.550: E/AndroidRuntime(1172):     at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-08 19:34:45.550: E/AndroidRuntime(1172):     at 
android.app.ActivityThread.access$600(ActivityThread.java:141)
08-08 19:34:45.550: E/AndroidRuntime(1172):     at   
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-08 19:34:45.550: E/AndroidRuntime(1172):     at  
android.os.Handler.dispatchMessage(Handler.java:99)
08-08 19:34:45.550: E/AndroidRuntime(1172):     at   
android.os.Looper.loop(Looper.java:137)
08-08 19:34:45.550: E/AndroidRuntime(1172):     at   
android.app.ActivityThread.main(ActivityThread.java:5041)
08-08 19:34:45.550: E/AndroidRuntime(1172):     at   
java.lang.reflect.Method.invokeNative(Native Method)
08-08 19:34:45.550: E/AndroidRuntime(1172):     at 
java.lang.reflect.Method.invoke(Method.java:511)
08-08 19:34:45.550: E/AndroidRuntime(1172):     at 

   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:793)
08-08 19:34:45.550: E/AndroidRuntime(1172):     at  
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-08 19:34:45.550: E/AndroidRuntime(1172):     at 
dalvik.system.NativeStart.main(Native Method)
08-08 19:34:45.550: E/AndroidRuntime(1172): Caused by: 
java.lang.ClassCastException: com.example.main.MainActivity cannot be cast 
to android.view.View$OnClickListener
08-08 19:34:45.550: E/AndroidRuntime(1172):     at    
com.example.main.MainActivity.addListenerOnButton(MainActivity.java:36)
08-08 19:34:45.550: E/AndroidRuntime(1172):     at 
com.example.main.MainActivity.onCreate(MainActivity.java:28)
08-08 19:34:45.550: E/AndroidRuntime(1172):     at 
android.app.Activity.performCreate(Activity.java:5104)
08-08 19:34:45.550: E/AndroidRuntime(1172):     at     
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-08 19:34:45.550: E/AndroidRuntime(1172):     at     
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-08 19:34:45.550: E/AndroidRuntime(1172):     ... 11 more

感谢您的建议 这让我很沮丧 我尝试了其他方法,但仍然让我对 8 按钮意图感到困惑

方法 How can i add another button intent beside previous one?

- 问题已解决 -

我的错误是没有在我的程序中添加“addListenerOnButton”。

但是这个链接可以解决多个活动

How to have multiple buttons in one activity

只需添加 .xml 代码

android:onClick="onClick"

并在 .java 中使用它

public void onClick(View v){

它对我有用。谢谢你的帮助。 :)

【问题讨论】:

  • 你上面的代码中的 addListenerOnButton 方法在哪里?它说崩溃发生在那里,但我在您上面发布的代码中没有看到任何此类方法。
  • 谢谢!!!!那是我的大问题。我不添加“addListenerOnButton”。我找到了另一种方法,不再需要 addListenerOnButton .. 感谢您的回答

标签: android eclipse button android-intent


【解决方案1】:

清理项目并重试。 how to clean android project

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-04
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    相关资源
    最近更新 更多