登录成功后如何显示另一个布局

时间:2015-07-15 05:29:11

标签: java android android-layout

我是Android新手,我有两个问题。

  1. 我想在登录成功时显示另一个布局。

  2. 其他部分无法使用onsuccess()方法。如果用户提供错误的用户名和密码意味着抛出错误消息,但是我收到错误:

    application stopped unfortunately

  3. 这是我的代码:

    LoginFragment.java

    public void onSuccess(int statusCode, Header[] headers, byte[] response) {
    
                    // Hide Progress Dialog
    
                    prgDialog.hide();
                    try {
                        // JSON Object
                        JSONObject obj = new JSONObject(new String(response));
                        // When the JSON response has status boolean value assigned with true
                        if (obj.getBoolean("status")) {
                            Toast.makeText(getActivity().getApplicationContext(), "You are successfully logged in!", Toast.LENGTH_LONG).show();
                            // Navigate to Home screen
                            navigatetoSearchActivity();
                        }
                        // Else display error message
                        else {
                            errorMsg.setText(obj.getString("error_msg"));
                            Toast.makeText(getActivity().getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        Toast.makeText(getActivity().getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
    
                    }
                }
    
    
                // When the response returned by REST has Http response code other than '200'
                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responsebody, Throwable error) {
                    // Hide Progress Dialog
                    prgDialog.hide();
                    // When Http response code is '404'
                    if (statusCode == 404) {
                        Toast.makeText(getActivity().getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
                    }
                    // When Http response code is '500'
                    else if (statusCode == 500) {
                        Toast.makeText(getActivity().getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
                    }
                    // When Http response code other than 404, 500
                    else {
                        Toast.makeText(getActivity().getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
                    }
    
                }
            });
        }
        /**
         * Method which navigates from Login Activity to Search Activity
         */
        public void navigatetoSearchActivity(){
            Intent searchIntent = new Intent(getActivity().getApplicationContext(),Search.class);
            //searchIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(searchIntent);
        }
    

    Search.java

    import android.app.Activity;
    import android.os.Bundle;
    public class Search extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //Displays Search Screen
            setContentView(R.layout.search);
        }
    }
    

    第一个问题logcat:

     java.lang.RuntimeException: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.h2o/com.h2o.Search}; have you declared this activity in your AndroidManifest.xml?
                at com.loopj.android.http.AsyncHttpResponseHandler.onUserException(AsyncHttpResponseHandler.java:313)
                at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:404)
                at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:184)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5221)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
         Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.h2o/com.h2o.Search}; have you declared this activity in your AndroidManifest.xml?
                at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1761)
                at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
                at android.app.Activity.startActivityForResult(Activity.java:3736)
                at android.app.Activity.startActivityForResult(Activity.java:3697)
                at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:829)
                at android.support.v4.app.Fragment.startActivity(Fragment.java:897)
                at com.h2o.LoginFragment$TabsAdapter.navigatetoSearchActivity(LoginFragment.java:278)
                at com.h2o.LoginFragment$TabsAdapter$3.onSuccess(LoginFragment.java:235)
                at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:360)
                at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:184)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5221)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
    

    第二个问题logcat:

    java.lang.RuntimeException: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at com.loopj.android.http.AsyncHttpResponseHandler.onUserException(AsyncHttpResponseHandler.java:313)
            at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:404)
            at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:184)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
            at com.h2o.LoginFragment$TabsAdapter$3.onSuccess(LoginFragment.java:239)
            at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:360)
            at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:184)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
    

5 个答案:

答案 0 :(得分:2)

错误清楚地显示在提供的logcat中 1)在Manifest中未声明活动。在活动标签中声明它。
2)您正在文本视图上设置文本而不进行初始化。
例如:TextView tv=(TextView)fingViewById(R.id.tv1);

答案 1 :(得分:2)

第一个问题

search activity的清单中添加条目。

<activity
    android:name="com.packagename.yourMainActivityName" >
     <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
</activity>

第二个问题: 检查以下可以解决问题的要点:

  

1.检查您的回答是否为空。

     

2.错误可以是JSONobject,您可以从中status检查obj是否为空。

     

3.检查您是否已声明并初始化errorMsg文本视图。

答案 2 :(得分:1)

将此<activity android:name="com.h2o.Search" ></activity>插入AndroidManifest.xml

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.stockquote.MainActivity"
        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="com.h2o.Search" >
    </activity>
</application>

答案 3 :(得分:1)

请在项目的清单文件中声明搜索类。

样品:

<activity android:name=".Search">   </activity>

答案 4 :(得分:1)

对于第一个问题,您似乎希望使用新布局开始新活动。请阅读Google tutorial,它对该主题非常有帮助(通过在IDE中添加活动,您无需自己编辑清单文件,其他答案将指导您)。

对于第二个问题,似乎你的errorMsg没有被初始化。你需要一些东西

errorMsg = (TextView) findViewById(R.id.errorMessageId);

findViewById()Activity类的方法,因此您可能需要使用getActivity()获取它。