2014-11-08 159 views
-2

我一直在嘗試這個很長一段時間了。每當我使用setonclicklistener時,應用程序永遠不會打開。開始之前崩潰。通過引用Android開發人員添加到按鈕的Onclick方法只是讓應用程序啓動。但是點擊按鈕後它會崩潰。添加onclick監聽器按鈕停止應用程序工作

這是我的MainActivity.java: package com.example.tyro;

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

public class MainActivity extends Activity { 

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; 
Button button; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.login); 

// Locate the button in activity_main.xml 
    button = (Button) findViewById(R.id.loginbutton); 

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

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

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 

    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.main, menu); 
    return super.onCreateOptionsMenu(menu); 
} 

@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(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

    } 

這是我showmainpage類

 package com.example.tyro; 

import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 

public class ShowMainPage extends ActionBarActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_show_main_page); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.show_main_page, 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(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
} 

這是我login.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/tyro_login" 
android:orientation="vertical" 
android:paddingLeft="30dp" 
android:paddingTop="50dp" 
tools:context="com.example.tyro.MainActivity" > 

<TextView 
    android:id="@+id/login_intro" 
    android:layout_width="104dp" 
    android:layout_height="wrap_content" 
    android:text="Welcome" 
    android:textColor="#ffffff" 
    android:textSize="20sp" 
    android:textStyle="bold" /> 

<TextView 
    android:id="@+id/login_text" 
    android:layout_width="327dp" 
    android:layout_height="wrap_content" 
android:text="Please enter your username and password " 
    android:textColor="#ffffff" 
    android:textSize="20sp" /> 

<EditText 
    android:id="@+id/login_user" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="30dp" 
    android:ems="10" 
    android:hint="Enter your username" 
    android:inputType="textPersonName" 
    android:textColor="#ffffff" 
    android:textColorHint="#ffffff" /> 

<EditText 
    android:id="@+id/login_pass" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:hint="Enter your password" 
    android:inputType="textPersonName" 
    android:paddingTop="10dp" 
    android:textColor="#ffffff" 
    android:textColorHint="#ffffff" /> 

<TextView 
    android:id="@+id/forgot_pass" 
    android:layout_width="267dp" 
    android:layout_height="wrap_content" 
    android:text="Forgot your username/password?" 
    android:textColor="#ffffff" 
    android:textSize="12sp" /> 

<Button 
    android:id="@+id/loginbutton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginTop="20dp" 
    android:background="#00000000" 
    android:text="LOGIN" 



    > 
</Button> 

<TextView 
    android:id="@+id/signup_text" 
    android:layout_width="300dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginTop="50dp" 
    android:gravity="center" 
    android:text="Don't have an account?Click here to sign up" 
    android:textColor="#ffffff" 
    android:textSize="12sp" /> 

<ImageButton 
    android:id="@+id/signup_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout_marginTop="10dp" 
    android:background="#00000000" 
    android:src="@drawable/signup_button" > 
</ImageButton> 

</LinearLayout> 

這是logcat的:

11-07 23:30:15.167: E/AndroidRuntime(16904): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tyro/com.example.tyro.ShowMainPage}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
11-07 23:30:15.167: E/AndroidRuntime(16904): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at android.app.ActivityThread.access$800(ActivityThread.java:135) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at android.os.Handler.dispatchMessage(Handler.java:102) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at android.os.Looper.loop(Looper.java:136) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at android.app.ActivityThread.main(ActivityThread.java:5001) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at java.lang.reflect.Method.invokeNative(Native Method) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at java.lang.reflect.Method.invoke(Method.java:515) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at dalvik.system.NativeStart.main(Native Method) 
11-07 23:30:15.167: E/AndroidRuntime(16904): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
11-07 23:30:15.167: E/AndroidRuntime(16904): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:147) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:139) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at com.example.tyro.ShowMainPage.onCreate(ShowMainPage.java:12) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at android.app.Activity.performCreate(Activity.java:5231) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
11-07 23:30:15.167: E/AndroidRuntime(16904): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) 
11-07 23:30:15.167: E/AndroidRuntime(16904): ... 11 more 
+0

請發佈你的logcat – 2014-11-08 06:47:58

+0

你有沒有在AndroidManifest.xml中聲明ShowMainPage? – 2014-11-08 06:48:41

+0

添加了logcat – 2014-11-08 07:02:24

回答

0

你login_user是EditText上

<EditText 
android:id="@+id/login_user" 

您不能將其轉換爲按鈕。在你的logcat

java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.Button 

更新:使用

public class ShowMainPage extends Activity {//.. 
+0

改變它爲loginbutton ..still相同的結果 – 2014-11-08 07:08:10

+0

顯示新的LogCat輸出 – 2014-11-08 07:09:22

+0

將其更改爲一個按鈕...第一個屏幕顯示up..after點擊它墜毀後...第一個錯誤在logcat是說類似的東西您需要使用Theme.AppCombat主題 – 2014-11-08 07:19:22

0

有你在你的AndroidManifest.xml中聲明ShowMainPage? 當你的應用程序崩潰時,你可以在你的logcat中找到詳細的原因。

0

這是EditText上

<EditText 
android:id="@+id/login_user" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginTop="30dp" 
android:ems="10" 
android:hint="Enter your username" 
android:inputType="textPersonName" 
android:textColor="#ffffff" 
android:textColorHint="#ffffff" /> 

美國R使用這個

button = (Button) findViewById(R.id.login_user); 
this is button not edittext so this regian u geting error 

檢查編輯文本按鈕在XML文件中

<Button 
    android:id="@+id/login_user" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 
0

只好改擴建ActionBarActivity到活動,並開始工作