Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按钮在Android应用程序中不起作用_Java_Android - Fatal编程技术网

Java 按钮在Android应用程序中不起作用

Java 按钮在Android应用程序中不起作用,java,android,Java,Android,我是Android新手,正在尝试实现一个简单的按钮,它应该在Toast中显示一条消息(最终将用于创建一个帐户)。但当我按下按钮时,什么也没有发生,logcat上也没有显示错误,所以我肯定错过了一些明显的东西,但我找不到 AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" pac

我是Android新手,正在尝试实现一个简单的按钮,它应该在Toast中显示一条消息(最终将用于创建一个帐户)。但当我按下按钮时,什么也没有发生,logcat上也没有显示错误,所以我肯定错过了一些明显的东西,但我找不到

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>
<?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>
Registration.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();

            }
        });
    }
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();
    }
}

我做错了什么?

您在
片段和
活动中膨胀
布局

如果要处理
片段中的
按钮
,请执行以下操作:

@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;
    }
您的
活动
中的代码看起来不错,但是,您为这两个活动扩展了相同的
布局

请参见:

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

setContentView(R.layout.registration);
View rootView = inflater.inflate(R.layout.registration, container, false);
另外,按CTRL+Alt+O删除未使用的导入,最后应该是这样的(您的
片段的导入):

就像我猜的,问题是因为你在一个
片段和一个
活动中膨胀了一个
布局
。我已经试过了,它在
活动中起作用

(顺便提一下,这个问题问得不错)


解决方案:
Fragment
Activity
设置自己的
Layout
,但两者的
Layout
不同


然后,你就可以开始了。

你的清单上说启动器活动是.Mainpage…你怎么能看到Registration.Java呢?他从主活动开始RegistrationActivity,这不是他的问题,他看到按钮并单击itI,建议使用
Log.d()
和/或
System.out.println()
将消息添加到日志中,以帮助您了解发生了什么。就我所知,您的代码看起来一切都很好。@LinX64我不确定您所说的“没有AppCompat活动无法运行”是什么意思。您是指appcompat支持库吗?如果操作正确,您当然可以创建一个从不使用此库的应用程序。请向我们显示您实际拥有的
AndroidManifest.xml
文件。正如@cricket_007所说,你的问题中的问题很多,这意味着它与你用来成功运行应用程序的问题不同。我们无法帮助您,除非我们有代码准确地重现您遇到的问题。如果我正确地阅读了问题,则该片段不会用于任何活动,虽然是的,但最有趣的部分是:这是另一个问题。哈哈,什么?无论如何,我希望这能解决这个问题:)也许是其他问题导致了这个问题