2016-02-04 12 views
5

Tôi mới sử dụng Android và cố gắng triển khai một nút đơn giản, sẽ hiển thị thông báo trong Bánh mì nướng (cuối cùng nó sẽ được sử dụng để tạo tài khoản). Nhưng không có gì xảy ra khi tôi nhấn nút, không có lỗi nào được hiển thị trên logcat hoặc vì vậy tôi phải thiếu một cái gì đó hiển nhiên nhưng tôi không thể tìm thấy nó !!Nút không hoạt động trong ứng dụng Android

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.laptop.whatsfordinner" > 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <!--Splash Screen --> 
    <activity 
     android:name=".Splash" 
     android:label="@string/app_name" 
     android:screenOrientation="portrait" 
     android:theme="@android:style/Theme.Black.NoTitleBar" > 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 

     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
    </activity> 

    <activity 
     android:name=".MainPage" 
     android:label="@string/app_name" > 

    </activity> 

    <!--Register User --> 
    <activity 
     android:name=".RegistrationActivity" 
     android:label="Register User"> 
    </activity> 
</application> 

<!-- Internet Permissions --> 
<uses-permission android:name="android.permission.INTERNET" /> 

</manifest> 

registration.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:weightSum="1"> 

    <!-- Name Label --> 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/reg_title1" 
     android:paddingLeft="10dip" 
     android:paddingRight="10dip" 
     android:paddingTop="10dip" 
     android:textSize="17sp"/> 

    <!-- Input Name --> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="2.08"></LinearLayout> 

    <EditText android:id="@+id/inputName" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dip" 
     android:layout_marginBottom="15dip" 
     android:singleLine="true" 
     android:background="#ffffff" /> 

    <!-- Username Label --> 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/reg_title2" 
     android:paddingLeft="10dip" 
     android:paddingRight="10dip" 
     android:paddingTop="10dip" 
     android:textSize="17sp"/> 

    <!-- Input Username --> 
    <EditText android:id="@+id/inputUsername" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dip" 
     android:layout_marginBottom="15dip" 
     android:singleLine="true" 
     android:background="#ffffff" /> 

    <!-- Email Label --> 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/reg_title3" 
     android:paddingLeft="10dip" 
     android:paddingRight="10dip" 
     android:paddingTop="10dip" 
     android:textSize="17sp"/> 

    <!-- Input Email --> 
    <EditText android:id="@+id/inputEmail" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dip" 
     android:layout_marginBottom="15dip" 
     android:singleLine="true" 
     android:inputType="textEmailAddress" 
     android:background="#ffffff" /> 

    <!-- Password Label --> 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/reg_title4" 
     android:paddingLeft="10dip" 
     android:paddingRight="10dip" 
     android:paddingTop="10dip" 
     android:textSize="17sp"/> 

    <!-- Input Password --> 
    <EditText android:id="@+id/inputPassword" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dip" 
     android:layout_marginBottom="15dip" 
     android:singleLine="true" 
     android:inputType="textPassword" 
     android:background="#ffffff" /> 

    <!-- Button Register--> 

    <Button android:id="@+id/btnRegister" 
     android:layout_width="136dp" 
     android:layout_height="37dp" 
     android:text="@string/reg_button1" 
     android:enabled="true" 
     android:layout_gravity="center_horizontal" /> 

    </LinearLayout> 

RegistrationActivity.java

package com.example.laptop.whatsfordinner; 


    import android.app.Activity; 
    import java.util.ArrayList; 
    import java.util.List; 

    import org.apache.http.NameValuePair; 
    import org.apache.http.message.BasicNameValuePair; 
    import org.json.JSONException; 
    import org.json.JSONObject; 

    import android.app.Activity; 
    import android.app.ProgressDialog; 
    import android.content.Intent; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.Toast; 



    public class RegistrationActivity extends Activity { 


    // Progress Dialog 
    private ProgressDialog pDialog; 

    JSONParser jsonParser = new JSONParser(); 
    EditText inputName; 
    EditText inputUsername; 
    EditText inputEmail; 
    EditText inputPassword; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.registration); 

     // Create button 
     Button btnRegister1 = (Button) findViewById(R.id.btnRegister); 

     // button click event 
     btnRegister1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       // creating new account in background thread 
       Toast.makeText(RegistrationActivity.this, "Example action.",  Toast.LENGTH_SHORT).show(); 

      } 
     }); 
    } 

Registration.java

package com.example.laptop.whatsfordinner; 

import android.app.Activity; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.support.v4.widget.DrawerLayout; 

public class Registration extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    private static final String ARG_SECTION_NUMBER = "section_number"; 

    /** 
    * Returns a new instance of this fragment for the given section 
    * number. 
    */ 
    public static Registration newInstance(int sectionNumber) { 
     Registration fragment = new Registration(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    public Registration() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.registration, container, false); 
     return rootView; 
    } 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     ((MainPage) activity).onSectionAttached(
       getArguments().getInt(ARG_SECTION_NUMBER)); 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
    } 
} 

Tôi đang làm gì sai?

+0

thuộc tính được bật này trong nút xml là gì? –

+0

mã có vẻ tốt. – thedarkpassenger

+0

được bật có nghĩa là nút có sẵn cho đầu vào của người dùng, nhưng từ mã tất cả mọi thứ có vẻ tốt – DZDomi

Trả lời

3

Bạn đang tăng thêm Layout trong một Fragment và trong một Activity.

Nếu bạn muốn xử lý mà Button trong Fragment của bạn, làm điều này:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.activity_main, container, false); 

     Button regButton1 = (Button) rootView.findViewById(R.id.btnRegister); 

     regButton1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Toast.makeText(getActivity(), "Example action", Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     return rootView; 
    } 

Mã trong Activity của bạn có vẻ tốt nhưng, bạn đã được thổi phồng cùng Layout cho cả hai.

Xem:

setContentView(R.layout.registration); 

Và:

View rootView = inflater.inflate(R.layout.registration, container, false); 

Ngoài ra, Nhấn CTRL + Alt + O để loại bỏ việc nhập khẩu không sử dụng, cuối cùng, nó nên như thế này (nhập khẩu của bạn Fragment):

import android.app.Activity; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.Toast; 

Và cũng giống như tôi đoán, vấn đề là bởi vì bạn đã thổi phồng một Layout trong một Fragment và trong một Activity .và tôi đã cố gắng đó và nó làm việc với Activity đó.

(câu hỏi hay bằng cách này).


Giải pháp: Đặt riêng bạn Layout cho Fragment và bạn Activity nhưng không giống nhau Layout cho cả hai.

Sau đó, bạn đã sẵn sàng.

+1

Nếu tôi đọc câu hỏi một cách chính xác, Fragment không được sử dụng trong bất kỳ hoạt động nào, mặc dù –

+0

Có chính xác, nhưng phần thú vị nhất là: http: // stackoverflow.com/questions/35207636/button-not-working-in-android-application?noredirect=1#comment58142292_35207636 và đó là một vấn đề/vấn đề khác. – Mohsen

+0

Haha, cái gì? Anyways ** hy vọng ** điều này giải quyết vấn đề –

Các vấn đề liên quan