无法使用按钮启动另一个活动

时间:2014-07-09 13:29:59

标签: java android android-intent android-activity

以下是我主要活动的代码:

package com.manparvesh.Soil;

import com.manparvesh.soilclassification.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class myMainScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.classificationsystems);

    Button aashtob = (Button) findViewById(R.id.aashto);
    aashtob.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v1){
            startActivity(new Intent(getApplicationContext(),AASHTO_Open.class));
        }
    });

    Button uscsb = (Button) findViewById(R.id.uscs);
    uscsb.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v2) {
            // TODO Auto-generated method stub
            startActivity(new Intent(getApplicationContext(),USCS_Open.class));
        }
    });

    Button usdab = (Button) findViewById(R.id.usda);
    usdab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v3) {
            // TODO Auto-generated method stub
            startActivity(new Intent(getApplicationContext(),USDA_Open.class));
        }
    });
}

}

这是我要尝试打开的类的代码:

package com.manparvesh.Soil;

import com.manparvesh.soilclassification.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.*;

public class AASHTO_Open extends Activity {
Button classify1,clear1,menu1;
EditText S10,S40,S200,ll,pi;
TextView CC,CdCd;
int c;
String C,Cd;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.aashto);

    classify1=(Button)findViewById(R.id.classify1);
    clear1=(Button)findViewById(R.id.clear1);
    menu1=(Button)findViewById(R.id.menu1);

    S10   = (EditText)findViewById(R.id.T11);
    S40   = (EditText)findViewById(R.id.T12);
    S200   = (EditText)findViewById(R.id.T13);
    ll   = (EditText)findViewById(R.id.T14);
    pi   = (EditText)findViewById(R.id.T15);


        int s10 = Integer.parseInt(S10.getText().toString());
        int s40 = Integer.parseInt(S40.getText().toString());
        int s200 = Integer.parseInt(S200.getText().toString());
        int LL = Integer.parseInt(ll.getText().toString());
        int PI = Integer.parseInt(pi.getText().toString());

        if (s10<=50){
            if (s40<=30){
                if (s200<=15 && PI<=6){
                    c=1;
                }
            }else if(s40<=50){
                if (s200<=25){
                    c=2;
                }
            }else{
                if (s200<=10){
                    c=3;
                }else if (s200<=35){
                    if (LL<=40){
                        if (PI<=10){
                            c=4;
                        }else{
                            c=5;
                        }
                    }else{
                        if (PI<=10){
                            c=6;
                        }else{
                            c=7;
                        }
                    }
                }else{
                    if (LL<=40){
                        if (PI<=10){
                            c=8;
                        }else{
                            c=9;
                        }
                    }else{
                        if (PI<=10){
                            c=10;
                        }else{
                            c=11;
                        }
                    }
                }
            }
        }


    if (("".equals(S10))|| ("".equals(S200)) || ("".equals(S40)) || ("".equals(LL)) || ("".equals(PI))){
        throw new NumberFormatException();
    }



    switch(c){
    case 1:
        C="A-1-a";
        Cd="stone fragments, gravel and sand";
        break;
    case 2:
        C="A-1-b";
        Cd="stone fragments, gravel and sand";
        break;
    case 3:
        C="A-3";
        Cd="fine sand";
        break;
    case 4:
        C="A-2-4";
        Cd="silty or clayey gravel and sand";
        break;
    case 5:
        C="A-2-6";
        Cd="silty or clayey gravel and sand";
        break;
    case 6:
        C="A-2-5";
        Cd="silty or clayey gravel and sand";
        break;
    case 7:
        C="A-2-7";
        Cd="silty or clayey gravel and sand";
        break;
    case 8:
        C="A-4";
        Cd="silty soils";
        break;
    case 9:
        C="A-6";
        Cd="clayey soils";
        break;
    case 10:
        C="A-5";
        Cd="silty soils";
        break;
    case 11:
        C="A-7";
        Cd="clayey soils";
        break;
    }


    classify1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v1) {
            CC.setText(C);
            CdCd.setText(Cd);
       }
    });
    menu1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v1) {
            startActivity(new Intent("com.tutorial.CLEARSCREEN"));
       }
    });
    clear1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v1) {
            S10.setText("");
            S40.setText("");
            S200.setText("");
            ll.setText("");
            pi.setText("");
            CC.setText("");
            CdCd.setText("");
       }
    });

}
}`

我正在尝试打开的布局的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="match_parent"
android:background="@drawable/bg3"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="105dp"
    android:textColor="#FFFFFF"
    android:text="AASHTO"
    android:textSize="55dp"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<LinearLayout
    android:layout_marginTop="5dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:textColor="#FFFFFF"
        android:id="@+id/textView2"
        android:layout_width="159dp"
        android:layout_height="wrap_content"
        android:text="Sieve no.10 (% passing)" />

    <EditText
        android:inputType="phone"
        android:id="@+id/T11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10" 
        android:background="#99FFFFFF">

        <requestFocus />
    </EditText>

</LinearLayout>

<LinearLayout
    android:layout_marginTop="5dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:textColor="#FFFFFF"
        android:id="@+id/textView4"
        android:layout_width="159dp"
        android:layout_height="wrap_content"
        android:text="Sieve no.40 (% passing)" />

    <EditText
        android:inputType="phone"
        android:background="#99FFFFFF"
        android:id="@+id/T12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10" />

</LinearLayout>

<LinearLayout
    android:layout_marginTop="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:textColor="#FFFFFF"
        android:id="@+id/aegg"
        android:layout_width="159dp"
        android:layout_height="wrap_content"
        android:text="Sieve no.200 (% passing)" />

    <EditText
        android:inputType="phone"
        android:background="#99FFFFFF"
        android:id="@+id/T13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10" />

</LinearLayout>

<LinearLayout
    android:layout_marginTop="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:textColor="#FFFFFF"
        android:id="@+id/textView6"
        android:layout_width="159dp"
        android:layout_height="wrap_content"
        android:text="Liquid Limit" />

    <EditText
        android:inputType="phone"
        android:background="#99FFFFFF"
        android:id="@+id/T14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10" />

</LinearLayout>

<LinearLayout
    android:layout_marginTop="5dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:textColor="#FFFFFF"
        android:id="@+id/textView3"
        android:layout_width="159dp"
        android:layout_height="wrap_content"
        android:text="Plasticity Index (=LL-PL)" />

    <EditText
        android:inputType="phone"
        android:background="#99FFFFFF"
        android:id="@+id/T15"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10" />

</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center" 
    android:layout_marginTop="5dp">

    <Button
        android:textColor="#FFFFFF"
        android:id="@+id/menu1"
        android:layout_width="105dp"
        android:layout_height="wrap_content"
        android:text="Menu" 
        android:gravity="center"
        android:layout_marginRight="0.75dp"
        android:background="@drawable/button_wood"
        />

    <Button
        android:textColor="#FFFFFF"
        android:id="@+id/clear1"
        android:layout_width="105dp"
        android:layout_height="wrap_content"
        android:text="Clear" 
        android:layout_marginRight="0.75dp"
        android:gravity="center"
        android:background="@drawable/button_wood"
        />

    <Button
        android:textColor="#FFFFFF"
        android:id="@+id/classify1"
        android:layout_width="105dp"
        android:layout_height="wrap_content"
        android:text="Classify"
        android:gravity="center"
        android:background="@drawable/button_wood"
         />

</LinearLayout>

</LinearLayout>

清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.manparvesh.soilclassification"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19"
    android:maxSdkVersion="19"  />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.manparvesh.Soil.MainActivity"
        android:label="@layout/splash" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.manparvesh.Soil.myMainScreen"     android:screenOrientation="portrait" android:theme="@style/AppTheme"     android:label="@layout/classificationsystems"></activity>
    <activity android:name="com.manparvesh.Soil.AASHTO_Open"     android:theme="@style/AppTheme" android:description="@layout/aashto"></activity>
    <activity android:name="com.manparvesh.Soil.USCS_Open"     android:label="@layout/uscs"></activity>
    <activity android:name="com.manparvesh.Soil.USDA_Open"     android:theme="@style/AppTheme" android:label="@layout/usda"></activity>

        </application>

</manifest>

这是LogCat显示的内容:

07-09 18:27:15.803: E/AndroidRuntime(686): FATAL EXCEPTION: main
07-09 18:27:15.803: E/AndroidRuntime(686): java.lang.RuntimeException: Unable to start         activity ComponentInfo{com.manparvesh.soilclassification/com.manparvesh.Soil.USCS_Open}:     java.lang.NumberFormatException: Invalid double: ""
07-09 18:27:15.803: E/AndroidRuntime(686):  at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
07-09 18:27:15.803: E/AndroidRuntime(686):  at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
07-09 18:27:15.803: E/AndroidRuntime(686):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
07-09 18:27:15.803: E/AndroidRuntime(686):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
07-09 18:27:15.803: E/AndroidRuntime(686):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 18:27:15.803: E/AndroidRuntime(686):  at android.os.Looper.loop(Looper.java:137)
07-09 18:27:15.803: E/AndroidRuntime(686):  at android.app.ActivityThread.main(ActivityThread.java:4745)
07-09 18:27:15.803: E/AndroidRuntime(686):  at java.lang.reflect.Method.invokeNative(Native Method)
07-09 18:27:15.803: E/AndroidRuntime(686):  at java.lang.reflect.Method.invoke(Method.java:511)
07-09 18:27:15.803: E/AndroidRuntime(686):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-09 18:27:15.803: E/AndroidRuntime(686):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-09 18:27:15.803: E/AndroidRuntime(686):  at dalvik.system.NativeStart.main(Native Method)
07-09 18:27:15.803: E/AndroidRuntime(686): Caused by: java.lang.NumberFormatException: Invalid double: ""
07-09 18:27:15.803: E/AndroidRuntime(686):  at java.lang.StringToReal.invalidReal(StringToReal.java:63)
07-09 18:27:15.803: E/AndroidRuntime(686):  at java.lang.StringToReal.parseDouble(StringToReal.java:248)
07-09 18:27:15.803: E/AndroidRuntime(686):  at java.lang.Double.parseDouble(Double.java:295)
07-09 18:27:15.803: E/AndroidRuntime(686):  at com.manparvesh.Soil.USCS_Open.onCreate(USCS_Open.java:42)
07-09 18:27:15.803: E/AndroidRuntime(686):  at android.app.Activity.performCreate(Activity.java:5008)
07-09 18:27:15.803: E/AndroidRuntime(686):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
07-09 18:27:15.803: E/AndroidRuntime(686):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
07-09 18:27:15.803: E/AndroidRuntime(686):  ... 11 more

我无法在这里找到问题。

4 个答案:

答案 0 :(得分:0)

每当开始新的Activity时,您都需要传递Activity的上下文。传递applicationContext()会增加范围,并可能在将来导致内存泄漏。所以为了避免这种改变:

startActivity(new Intent(myMainScreen.this,AASHTO_Open.class));

这会将范围缩小到Activity

您收到的是NumberFormatException,因为当您尝试从TextView读取时,它是空的但您期望double值。因此,您必须始终对此进行检查并避免崩溃。你必须这样做:

if(S10.getText().toString() != null && S10.getText().toString().trim().length() > 0){
 int s10 = Integer.parseInt(S10.getText().toString());
}

在继续编写代码之前,还要看一下 Java编码标准

Java Coding Style

答案 1 :(得分:0)

当您尝试

时,您获得了NumberFormatException
 int s10 = Integer.parseInt(S10.getText().toString());

S10目前为空,因为您在创建时尚未输入,因此您尝试执行此操作

Integer.parseInt("");

因此得到NumberFormatException

答案 2 :(得分:0)

检查以下代码 -

        int s10 = Integer.parseInt(S10.getText().toString());
        int s40 = Integer.parseInt(S40.getText().toString());
        int s200 = Integer.parseInt(S200.getText().toString());
        int LL = Integer.parseInt(ll.getText().toString());
        int PI = Integer.parseInt(pi.getText().toString());

抛出异常,因为其中一个字段值(s10,s40,s200,LL,PI之外)为空白。你正在对该字段值进行Integer.parseInt("");

<强> NumberFormatException的
   如果无法将字符串解析为整数值。

解决方案1 ​​ -

将块放在try-catch -

        int s10 = 0, s40 = 0, s200 = 0, LL = 0, PI = 0;

        try{
        s10 = Integer.parseInt(S10.getText().toString());
        s40 = Integer.parseInt(S40.getText().toString());
        s200 = Integer.parseInt(S200.getText().toString());
        LL = Integer.parseInt(ll.getText().toString());
        PI = Integer.parseInt(pi.getText().toString());
        }
        catch(NumberFormatException e)
        {
          e.printStackTrace();
        }

解决方案2 -

将此if块放在EditText findView声明之后,然后执行某些操作  你的空白值。

if (("".equals(S10.getText().toString()))|| 
    ("".equals(S200.getText().toString())) || 
    ("".equals(S40.getText().toString())) || 
    ("".equals(LL.getText().toString())) || 
    ("".equals(PI.getText().toString())))
{
    //EditText field value is blank Do something
}

您可以为空白字段指定有效的整数值,以便它不会给出NumberFormatException。这是推荐的解决方案。

答案 3 :(得分:0)

Try this...

package com.manparvesh.Soil;

import com.manparvesh.soilclassification.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class myMainScreen extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.classificationsystems);

    Button aashtob = (Button) findViewById(R.id.aashto);
    aashtob.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v1){
        Intent i = new Intent(myMainScreen.this,AASHTO_Open.class);
            startActivity(i);
        }
    });

    Button uscsb = (Button) findViewById(R.id.uscs);
    uscsb.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v2) {
            // TODO Auto-generated method stub
            Intent i1 = new Intent(myMainScreen.this,USCS_Open.class);
            startActivity(i1);

        }
    });

    Button usdab = (Button) findViewById(R.id.usda);
    usdab.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v3) {
            // TODO Auto-generated method stub
            Intent i2 = new Intent(myMainScreen.this,USDA_Open.class);
            startActivity(i2);

        }
    });
}

}