簡體   English   中英

應用程式停止運作

[英]App stopped working

我正在嘗試運行該程序.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) {

    }
}
Spinner spin1=(Spinner)findViewById(R.id.spinner1);
Spinner spin2=(Spinner)findViewById(R.id.spinner2);

必須在setContentView之后進入onCreate ,這有兩個原因

  1. 您需要有效的上下文才能使用findViewById
  2. 沒有setContentView您就沒有小部件可以找到,因此無需調用findViewById

剛剛在我的代碼中進行了此更改,並且效果很好。

     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) {

        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM