切换视图android时出现意外错误

时间:2012-07-13 03:32:34

标签: java android eclipse sdk

我面临着一个奇怪的问题,不知道这是什么原因。 我有3个观看次数

ConsumerRegistration.java->registration.xml
Login.java->main2.xml
Toc.java->terms.xml

每当我独立运行3个视图时没有问题,当我从登录或从Toc登录调用Toc时,也没有问题,但是当我调用ConsumerRegistration形式Toc或Login时,它会抛出意外错误导致应用程序关掉 。 java和布局文件的代码如下所示

Login.java

    package com.multiview.org;
    import android.app.Activity;  
    import android.content.Intent;  
    import android.os.Bundle;  
    import android.view.View;  
    import android.widget.Button;  
     public class Login extends Activity {  
        /** Called when the activity is first created. */  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
          super.onCreate(savedInstanceState);  
          setContentView(R.layout.main2);  
          Button next = (Button) findViewById(R.id.button2);  
                next.setOnClickListener(new View.OnClickListener() {  
                 public void onClick(View view) {  

                  Intent myIntent = new Intent(view.getContext(), ConsumerRegistration.class);  
                   startActivityForResult(myIntent, 0);  
                 }  
               });  
        }  
      }  


main2.xml

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

        <Button android:text="View 1"  
              android:id="@+id/button2"  
             android:layout_width="fill_parent"  
              android:layout_height="wrap_content">  
          </Button>  

    </LinearLayout>

ConsumerRegistration.java

    package com.multiview.org;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicHeader;
    import org.apache.http.protocol.HTTP;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.Toast;

    public class ConsumerRegistration extends Activity {
        /** Called when the activity is first created. */
        EditText un,pw,pc,by,zc;
        RadioButton m,f;
        JSONObject j;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.registration);

             un = (EditText)findViewById(R.id.et_un);
             pw = (EditText)findViewById(R.id.et_pw);
             pc = (EditText)findViewById(R.id.et_pc);
             by = (EditText)findViewById(R.id.et_by);
             zc = (EditText)findViewById(R.id.et_zc);
             m=(RadioButton)findViewById(R.id.rb_m);
             f=(RadioButton)findViewById(R.id.rb_f);



            Button ok = (Button)findViewById(R.id.btn_login);
            ok.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    j = new JSONObject();
                    try {
                    j.put("username", un.getText().toString());
                    j.put("password", pw.getText().toString());
                    j.put("email", un.getText().toString());
                    j.put("is_active", "1");
                    j.put("is_staff", "0");
                    j.put("is_superuser","0");
                    j.put("promo_code", pc.getText().toString());
                    j.put("gender", "m");
                    j.put("zip", zc.getText().toString());
                    j.put("birth_year", by.getText().toString());
                    j.put("first_name", "john");
                    j.put("last_name", "doe");
                    j.put("current_state", "1");


                    } catch (JSONException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://10.0.2.2:8000/api/ecp/user/?format=json");
                    try {
                        HttpEntity entity;
                        StringEntity s = new StringEntity(j.toString());
                        s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                        entity = s;
                        httppost.setEntity(entity);
                        HttpResponse response;
                        response = httpclient.execute(httppost);
                        entity = response.getEntity();
                        InputStream is = entity.getContent();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                        StringBuilder sb = new StringBuilder();

                        String line = null;
                        try {
                            while ((line = reader.readLine()) != null) {
                                sb.append((line + "\n"));
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                is.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        try {
                            Toast.makeText(ConsumerRegistration.this, "created", Toast.LENGTH_SHORT).show();
                            JSONObject jsonObject = new JSONObject(sb.toString());
                            JSONObject meta = jsonObject.getJSONObject("meta");  
                            String limit = meta.getString("limit");  
                            Toast.makeText(ConsumerRegistration.this, limit, Toast.LENGTH_SHORT).show();
                            JSONArray array = jsonObject.getJSONArray("objects");

                            String key = array.getJSONObject(0).getString("api_key");
                            String uname = array.getJSONObject(0).getString("username");
                            Toast.makeText(ConsumerRegistration.this, j.toString(),
                                    Toast.LENGTH_SHORT).show();

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }   
                        //Toast.makeText(HelloWorldActivity.this, sb.toString(), Toast.LENGTH_SHORT).show();
                    } catch (ClientProtocolException e) {
                        Toast.makeText(ConsumerRegistration.this, e.toString(), Toast.LENGTH_SHORT).show();

                        // TODO Auto-generated catch block
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        Toast.makeText(ConsumerRegistration.this, e.toString(), Toast.LENGTH_SHORT).show();

                    }


                }
            });

            Button next = (Button) findViewById(R.id.btnLogin);  
            next.setOnClickListener(new View.OnClickListener() {  
             public void onClick(View view) {  
               Intent myIntent = new Intent(view.getContext(), Login.class);  
               startActivityForResult(myIntent, 0);  
             }  
           });
        }

    }

Registration.xml

    <TableLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        xmlns:android="http://schemas.android.com/apk/res/android">
        <TableRow>
            <Button 
                android:id="@+id/btnLogin"
                android:text="Login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>
        <TableRow>
            <TextView
            android:id="@+id/tv_un"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="Login Email:"
            android:textColor="#444444"
            android:textSize="10pt" />

            <EditText
                android:id="@+id/et_un"
                android:layout_width="150dip"
                android:layout_height="wrap_content"
                android:layout_alignTop="@id/tv_un"
                android:layout_toRightOf="@id/tv_un"
                android:background="@android:drawable/editbox_background" />
        </TableRow>
        <TableRow>
            <TextView
                android:id="@+id/tv_pw"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignRight="@+id/textView2"
                android:layout_below="@+id/et_un"
                android:text="Password:"
                android:textColor="#444444"
                android:textSize="10pt" />
        <EditText
            android:id="@+id/et_pw"
            android:layout_width="150dip"
            android:layout_height="wrap_content"
            android:background="@android:drawable/editbox_background"
            android:ems="10"
            android:password="true" />  
         </TableRow>
         <TableRow>
             <TextView
                android:id="@+id/tv_pc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/et_pc"
                android:layout_alignBottom="@+id/et_pc"
                android:layout_alignParentLeft="true"
                android:text="Promo Code"
                android:textColor="#444444"
                android:textSize="10pt" />
            <EditText
                android:id="@+id/et_pc"
                android:layout_width="150dip"
                android:layout_height="wrap_content"
                android:layout_alignTop="@id/tv_un"
                android:layout_toRightOf="@id/tv_un"
                android:background="@android:drawable/editbox_background" />

         </TableRow>
         <TableRow>
             <TextView
                android:id="@+id/tv_g"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="Gender"
                android:textColor="#444444"
                android:textSize="10pt" />
                 <RadioGroup
                    android:id="@+id/radioSex"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <RadioButton
                        android:id="@+id/rb_m"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checked="true"
                        android:text="Male" />

                    <RadioButton
                        android:id="@+id/rb_f"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Female" />

                  </RadioGroup>


         </TableRow>
         <TableRow>

             <TextView
                 android:id="@+id/textView2"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="Birth Year"
                 android:textColor="#444444"
                 android:textSize="10pt" />

          <EditText
            android:id="@+id/et_by"
            android:layout_width="150dip"
            android:layout_height="wrap_content"
            android:layout_alignTop="@id/tv_un"
            android:layout_toRightOf="@id/tv_un"
            android:background="@android:drawable/editbox_background" />

         </TableRow>
         <TableRow>
             <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/et_zc"
                android:layout_alignBottom="@+id/et_zc"
                android:layout_alignLeft="@+id/textView2"
                android:text="Zip Code"
                android:textColor="#444444"
                android:textSize="10pt" />

             <EditText
                android:id="@+id/et_zc"
                android:layout_width="150dip"
                android:layout_height="wrap_content"
                android:layout_alignTop="@id/tv_un"
                android:layout_toRightOf="@id/tv_un"
                android:background="@android:drawable/editbox_background" />

         </TableRow>
         <TableRow>
                 <Button
                    android:id="@+id/btn_back"
                    android:layout_width="100dip"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:text="Back" />


                 <Button
                    android:id="@+id/btn_login"
                    android:layout_width="100dip"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:text="Next" />


         </TableRow>
    </TableLayout>

Toc.java

    package com.multiview.org;
    import android.app.Activity;  
    import android.content.Intent;  
    import android.os.Bundle;  
    import android.view.View;  
    import android.widget.Button;  
     public class Toc extends Activity {  
        /** Called when the activity is first created. */  
        @Override  
        public void onCreate(Bundle savedInstanceState) {  
          super.onCreate(savedInstanceState);  
          setContentView(R.layout.terms);  
          Button next = (Button) findViewById(R.id.btnAccept);  
                next.setOnClickListener(new View.OnClickListener() {  
                 public void onClick(View view) {  
                   Intent myIntent = new Intent(view.getContext(), Login.class);  
                   startActivityForResult(myIntent, 0);  
                 }  
               });  
        }  
      }  


terms.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        xmlns:android="http://schemas.android.com/apk/res/android">
        <TableRow>

            <EditText
                android:id="@+id/EditText02"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:gravity="top|left"
                android:inputType="textMultiLine"
                android:lines="5"
                android:scrollHorizontally="false" 
                android:value="this is test"/>
        </TableRow>
        <TableRow>

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Back" />


            <Button
                android:id="@+id/btnAccept"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Accept" />
        </TableRow>
    </TableLayout>

Manifest file


    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.multiview.org"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk android:minSdkVersion="10" />
        <uses-permission android:name="android.permission.INTERNET" />

        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".Toc"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

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

    </manifest>

请告诉我如何找到确切错误的地方......谢谢

1 个答案:

答案 0 :(得分:2)

您尚未在清单中声明ConsumerRegistration活动。声明并再试一次。