Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.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 应用程序停止工作_Java_Android_Android Layout - Fatal编程技术网

Java 应用程序停止工作

Java 应用程序停止工作,java,android,android-layout,Java,Android,Android Layout,我正在尝试运行此程序。在logcat或生成过程中没有显示错误。但我的设备上出现了错误: 不幸的是,我的应用程序已停止工作 我如何克服这个问题 //activity.xml <RelativeLayout xmlns:android="http://schemas.android

我正在尝试运行此程序。在logcat或生成过程中没有显示错误。但我的设备上出现了错误:

不幸的是,我的应用程序已停止工作

我如何克服这个问题

//activity.xml

<RelativeLayout                                                                                                             xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"
            android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin" 
            tools:context=".MainActivity"
            android:background="#daf1ff">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="USER REGISTRATION"
                android:textSize="30dp"
                android:layout_marginBottom="20dp"
                android:layout_marginLeft="22dp"
                android:textColor="#ed2d26"/>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp">
                <TextView android:text="Name: "
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="30dp"
                    android:textColor="#fd7214"/>
                <EditText
                    android:layout_width="250dp"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp">
                <TextView android:text="Gender: "
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="30dp"
                    android:layout_marginRight="20dp"
                    android:textColor="#fd7214"/>
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Male"
                    android:textColor="#a61f82"
                    android:textSize="20dp"
                    android:layout_marginLeft="10dp"/>
                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Female"
                    android:textColor="#a61f82"
                    android:textSize="20dp"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp">
                <TextView android:text="Country: "
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="30dp"
                    android:textColor="#fd7214"
                    android:layout_marginRight="20dp"/>
                <Spinner
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/spinner1"
                    android:layout_marginTop="20dp">
                </Spinner>
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp">
                <TextView android:text="Address: "
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="30dp"
                    android:layout_marginRight="20dp"
                    android:textColor="#fd7214"/>
                <EditText
                    android:layout_width="250dp"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp">
                <TextView android:text="Phone No: +91 "
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="30dp"
                    android:textColor="#fd7214"
                    />
                <EditText
                    android:layout_width="150dp"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="50dp">
                <TextView android:text="E-mail: "
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="30dp"
                    android:textColor="#fd7214"
                    />
                <EditText
                    android:layout_width="86dp"
                    android:layout_height="wrap_content" />
                <Spinner
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/spinner2"
                    android:layout_marginTop="20dp">
                </Spinner>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text=".com "
                    android:textSize="20dp"
                    android:textColor="#ff0080"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="REGISTER"
                    android:textSize="25dp"
                    android:layout_marginRight="70dp"
                    android:id="@+id/register"/>
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="CANCEL"
                    android:textSize="25dp"
                    android:id="@+id/cancel"/>
            </LinearLayout>

        </LinearLayout>

    </RelativeLayout>

//MainActivity.java

package com.example.sreelakshmi.myuser;

    import android.app.Activity;
    import android.support.v7.app.ActionBarActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.Spinner;
    import android.widget.Toast;


    public class MainActivity extends Activity implements      AdapterView.OnItemSelectedListener {

    String[] country=new String[]{"India","Japan","Canada","US","Others"};
    String[] mailid=new String[]{"@ gmail","@ yahoo","@ rediffmail","@ hotmail","@ godrej"};
    Spinner spin1=(Spinner)findViewById(R.id.spinner1);
    Spinner spin2=(Spinner)findViewById(R.id.spinner2);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ArrayAdapter a1=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,country);
        a1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin1.setAdapter(a1);
        ArrayAdapter a2=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,mailid);
        a2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin2.setAdapter(a2);
        Button b1=(Button)findViewById(R.id.register);
        Button b2=(Button)findViewById(R.id.cancel);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"Registration Successful!",Toast.LENGTH_LONG).show();
            }
        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        spin1.setOnItemSelectedListener(this);
        spin2.setOnItemSelectedListener(this);
    }

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

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                               long arg3) {
        switch(arg0.getId()){
            case R.id.spinner1 :
                Toast.makeText(getApplicationContext(),country[arg2],Toast.LENGTH_LONG).show();
                break;
            case R.id.spinner2 :
                //Your Another Action Here.
                Toast.makeText(getApplicationContext(),mailid[arg2],Toast.LENGTH_LONG).show();
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
package com.example.sreelakshmi.myuser;
导入android.app.Activity;
导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.Spinner;
导入android.widget.Toast;
公共类MainActivity扩展活动实现AdapterView.OnItemSelectedListener{
字符串[]国家=新字符串[]{“印度”、“日本”、“加拿大”、“美国”、“其他”};
String[]mailid=新字符串[]{“@gmail”、“@yahoo”、“@rediffmail”、“@hotmail”、“@godrej”};
微调器spin1=(微调器)findViewById(R.id.spinner1);
Spinner spin2=(Spinner)findViewById(R.id.Spinner 2);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter a1=新的ArrayAdapter(这是android.R.layout.simple\u spinner\u dropdown\u项目,国家/地区);
a1.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
spin1.setAdapter(a1);
ArrayAdapter a2=新的ArrayAdapter(这是android.R.layout.simple\u spinner\u dropdown\u项,mailid);
a2.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
spin2.setAdapter(a2);
按钮b1=(按钮)findViewById(R.id.register);
按钮b2=(按钮)findViewById(R.id.cancel);
b1.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Toast.makeText(getApplicationContext(),“注册成功!”,Toast.LENGTH_LONG.show();
}
});
b2.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
完成();
}
});
spin1.setOnItemSelectedListener(此);
spin2.setOnItemSelectedListener(此);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
@凌驾
已选择公共视图(适配器视图arg0、视图arg1、内部arg2、,
长arg3){
开关(arg0.getId()){
案例R.id.喷丝头1:
Toast.makeText(getApplicationContext(),country[arg2],Toast.LENGTH\u LONG.show();
打破
案例R.id.喷丝头2:
//你的另一个动作在这里。
Toast.makeText(getApplicationContext(),mailid[arg2],Toast.LENGTH\u LONG.show();
打破
}
}
@凌驾
未选择公共无效(AdapterView父级){
}
}
由于两个原因,必须在
setContentView
之后的
onCreate

  • 您需要有效的上下文才能使用
    findviewbyd
  • 如果没有
    setContentView
    ,您就没有要查找的小部件,因此不需要调用
    findViewById

  • 只是在我的代码中做了这样的更改,效果很好。 . . .

    final Spinner spin1=(Spinner)findViewById(R.id.spinner1);
    ArrayAdapter a1=新的ArrayAdapter(这是android.R.layout.simple\u spinner\u dropdown\u项目,国家/地区);
    a1.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
    spin1.setAdapter(a1);
    最终微调器微调2=(微调器)findViewById(R.id.spinner2);
    ArrayAdapter a2=新的ArrayAdapter(这是R.layout.first,R.id.text1,mailid);
    spin2.setAdapter(a2);
    .
    .
    .
    .
    spin1.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
    @凌驾
    已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
    Toast.makeText(getApplicationContext(),spin1.getSelectedItem().toString(),Toast.LENGTH_LONG).show();
    }
    @凌驾
    未选择公共无效(AdapterView父级){
    }
    });
    spin2.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
    @凌驾
    已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
    Toast.makeText(getApplicationContext(),spin2.getSelectedItem().toString(),Toast.LENGTH_LONG).show();
    }
    @凌驾
    未选择公共无效(AdapterView父级){
    }
    });
    
    。 . . .
    .

    我无法理解错误的原因。我不太确定这是否是使用多个微调器的正确方法。所以我想
    Spinner spin1=(Spinner)findViewById(R.id.spinner1);
    Spinner spin2=(Spinner)findViewById(R.id.spinner2);
    
         final Spinner spin1=(Spinner)findViewById(R.id.spinner1);  
         ArrayAdapter a1=new ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item,country);
    a1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spin1.setAdapter(a1); 
    final Spinner spin2=(Spinner)findViewById(R.id.spinner2); 
    ArrayAdapter a2=new ArrayAdapter(this,R.layout.first,R.id.text1,mailid); 
    spin2.setAdapter(a2); 
     .
     .
     .
     .
     spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
                Toast.makeText(getApplicationContext(), spin1.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
    
            }
        });
        spin2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
                Toast.makeText(getApplicationContext(), spin2.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
    
            }
        });