java - android 切换 View 时出现意外错误

标签 java android eclipse sdk

我面临着奇怪的问题,不知道是什么造成的。 我有 3 次浏览

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

每当我独立运行 3 个 View 时,都没有问题,当我从 Login 调用 Toc 或从 Toc 登录时,也没有问题,但是当我从 Toc 或 Login 调用 ConsumerRegistration 表单时,它会抛出意外错误,导致应用程序关闭 。 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>

请让我如何找到意外错误的具体位置...谢谢

最佳答案

您尚未在 list 中声明 ConsumerRegistration Activity。声明它并重试。

关于java - android 切换 View 时出现意外错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11463770/

相关文章:

android - ListView 上的 setEmptyView 未在 android 应用程序中显示其 View ?

java - 在 Eclipse 中读取项目中的 .txt 文件

Eclipse:XXX 不是 YYY 中的有效行号

java - 使用@RestClientTest 对 rest 客户端进行 Spring boot 测试

java - 在 Java 中处理不适合 RAM 的大型数组

java - 在 SWT 中将多个图像加载到滚动合成中

android - 在选定的 ListView 上设置背景?

android - 如何在 Material 设计中更改操作栏菜单项文本颜色

android - 如何从 Cocos2dx 的 HelloWorld 示例创建 .apk?

java - 搜索算法(已经实现了排序算法)