2017-08-11 69 views
-6

我寫了一個簡單的警報對話框代碼。點擊按鈕時,警告框被創建。但我的應用程序崩潰,因爲我試圖運行它。 我的鑄造方法有問題嗎?我已經清理並構建了該項目,但它對我沒有任何作用。我是android開發新手。應用程序車道

主要業務代碼

package com.example.user.alert_dialog; 

import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 


public class MainActivity extends AppCompatActivity { 
    private Button bt; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     bt=(Button)findViewById(R.id.button); 


     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
       buttonlistener(); 
      } 
     }); 
    } 
    public void buttonlistener(){ 
     bt=(Button)findViewById(R.id.button); 
     bt.setOnClickListener(


      new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        AlertDialog.Builder a_builder = new AlertDialog.Builder(MainActivity.this); 
        a_builder.setMessage("Do you Want To Close This App!!!").setCancelable(false) 
          .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialog, int which) { 
            finish(); 
           } 
          }) 
          .setNegativeButton("No", new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialog, int which) { 
            dialog.cancel(); 
           } 
          }); 
       } 
      } 
    ); 
} 

@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的

E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.user.alert_dialog, PID: 7068 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.alert_dialog/com.example.user.alert_dialog.MainActivity}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
    Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.Button 
    at com.example.user.alert_dialog.MainActivity.onCreate(MainActivity.java:23) 
    at android.app.Activity.performCreate(Activity.java:6237) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
    at android.app.ActivityThread.-wrap11(ActivityThread.java)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:148)  
    at android.app.ActivityThread.main(ActivityThread.java:5417)  
    at java.lang.reflect.Method.invoke(Native Method)  
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  

content_main XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.user.alert_dialog.MainActivity" 
    tools:showIn="@layout/activity_main" 
    android:id="@+id/relativeLayout"> 
    final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout); 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Alert" 
     android:id="@+id/button" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="43dp" 
     android:onClick="setContentView" /> 
</RelativeLayout> 

主要業務XML

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.user.alert_dialog.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <include 
     android:id="@+id/button" 
     layout="@layout/content_main" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 
+5

如果你閱讀崩潰日誌,你會看到什麼問題 –

+0

我認爲問題在'bt =(Button)findViewById(R.id.button);'line。檢查你的按鈕ID是否給予任何按鈕或相關佈局 –

+1

'由於:java.lang.ClassCastException:android.widget.RelativeLayout無法轉換爲android.widget.Button'您無法將橙色轉換爲菠蘿。除非你是魔術師。 –

回答

1

你崩潰日誌中明確指出,你得到這個例外 -

java.lang.ClassCastException:android.widget.RelativeLayout不能 投地android.widget.Button

還規定其發生在哪一行

com.example.user.alert_dialog.MainActivity.onCreate(MainActivity.java:23)

這意味着您正試圖將RelativeLayout類型的視圖投射到Button

,你在該行做在你的onCreate -

bt=(Button)findViewById(R.id.button); 

所以請去檢查你的R.id.buttonRelativeLayout ID在XML文件中activity_main

+1

您不必向懶惰用戶提供勺子餵食答案,讓他們自己解決它 –

+0

我不能在R.id.button中看到我的相對佈局中的問題 –

+0

@MuhammadBilal在你的xml中有很多錯誤。我不會解決它,但給你一個提示。你有用xml運行的java代碼。我不確定它是否出現粘貼錯誤或實際代碼。然後在按鈕上點擊你有一個對setContentView的調用,這在很多層次上都是錯誤的。所以糾正你的XML,事情可能開始工作。請參閱android –

0

在您的MainActivity類別你是設置(activity_main)作爲你的佈局,但你的按鈕是在你在這裏發佈的(content_main.xml)中定義的。

在MainActivity變化

的setContentView(R.layout.activity_main);

的setContentView(R.layout.content_main);解決你的問題。

而且下面的代碼在您的主要活動發表評論

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
      buttonlistener(); 
     } 
    }); 

您的代碼有那麼多愚蠢的錯誤。你需要學習android基礎知識。

+0

中的佈局設計的基本過程我已發佈活動主代碼 –

+0

是啊兄弟關於愚蠢的錯誤 –

+0

@Shuhendra Singh感謝您的幫助 –