2017-07-17 6 views
0

あたりのように私は、アプリを作っていますポートレートモードにfragmentA(リストビューを含む)と遠景モードを使用する場合を示しており、それがfragmentAとfragmentB(ListViewコントロールとそれに対応する記述).ITがクラッシュし続け示し、私の勘では、どちらかと言うそこに障害がMainActivityであれば、他の状態にあるか、問題が通信しているpattern.Iヘルプを喜ばせるためにみんなを要求します。ここに私のMainActivity.xmlは、コードの残りの部分は柔軟なモジュラーUIオリエンテーション変更

<?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"> 

    <fragment 
     android:id="@+id/fragment" 
     android:name="com.example.prince.fragment_modularui2.FragmentA" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentStart="true" /> 

    <fragment 
     android:id="@+id/fragment2" 
     android:name="com.example.prince.fragment_modularui2.FragmentB" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentTop="true" /> 
</LinearLayout> 

風景の中に、ここで

package com.example.prince.fragment_modularui2; 

import android.content.Intent; 
import android.content.res.Resources; 

import android.support.v4.app.FragmentManager; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

public class MainActivity extends AppCompatActivity implements FragmentA.Communicator{ 
    String s; 
    Resources r=getResources(); 
    String[] a=r.getStringArray(R.array.descr); 



    FragmentA fa; 
    FragmentB fb; 
    FragmentManager manager; 


    @Override 
    protected void onCreate (Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     manager=getSupportFragmentManager(); 
     //fa always present 
     fa= (FragmentA) manager.findFragmentById(R.id.fragment); 
     fb= (FragmentB) manager.findFragmentById(R.id.fragment2); 
     fa.setCommunicator(this); 
    } 

    @Override 
    public void respond(int data) { 

      Intent i=new Intent(this,AnotherActivity.class); 
      i.putExtra("index",data); 
      startActivity(i); 



    } 
} 

activity_main.xmlはポートレートに

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


    <fragment 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:name="com.example.prince.fragment_modularui2.FragmentA" 
     android:id="@+id/fragment" 
     ></fragment> 


</LinearLayout> 

あるactivity_main https://github.com/AlmightyHeathcliff/Fragment_ModularUi2

スタックトレースがされています

07-18 09:45:09.252 1974-1974/? D/AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<< 
07-18 09:45:09.254 1974-1974/? D/AndroidRuntime: CheckJNI is OFF 
07-18 09:45:09.289 1974-1974/? W/art: Unexpected CPU variant for X86 using defaults: x86 
07-18 09:45:09.301 1974-1974/? D/ICU: No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat 
07-18 09:45:09.316 1974-1974/? E/memtrack: Couldn't load memtrack module (No such file or directory) 
07-18 09:45:09.316 1974-1974/? E/android.os.Debug: failed to load memtrack module: -2 
07-18 09:45:09.317 1974-1974/? I/Radio-JNI: register_android_hardware_Radio DONE 
07-18 09:45:09.325 1974-1974/? D/AndroidRuntime: Calling main entry com.android.commands.pm.Pm 
07-18 09:45:09.330 1974-1974/? I/art: System.exit called, status: 0 
07-18 09:45:09.330 1974-1974/? I/AndroidRuntime: VM exiting with result code 0. 
07-18 09:45:09.333 1974-1982/? W/MessageQueue: Handler (android.os.Handler) {f6bbfb3} sending message to a Handler on a dead thread 
               java.lang.IllegalStateException: Handler (android.os.Handler) {f6bbfb3} sending message to a Handler on a dead thread 
                at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543) 
                at android.os.Handler.enqueueMessage(Handler.java:643) 
                at android.os.Handler.sendMessageAtTime(Handler.java:612) 
                at android.os.Handler.sendMessageDelayed(Handler.java:582) 
                at android.os.Handler.post(Handler.java:338) 
                at android.os.ResultReceiver$MyResultReceiver.send(ResultReceiver.java:57) 
                at com.android.internal.os.IResultReceiver$Stub.onTransact(IResultReceiver.java:58) 
                at android.os.Binder.execTransact(Binder.java:565) 
07-18 09:45:09.972 1993-1993/? D/AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<< 
07-18 09:45:09.976 1993-1993/? D/AndroidRuntime: CheckJNI is OFF 
07-18 09:45:10.012 1993-1993/? W/art: Unexpected CPU variant for X86 using defaults: x86 
07-18 09:45:10.015 1993-1993/? D/ICU: No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat 
07-18 09:45:10.049 1993-1993/? E/memtrack: Couldn't load memtrack module (No such file or directory) 
07-18 09:45:10.049 1993-1993/? E/android.os.Debug: failed to load memtrack module: -2 
07-18 09:45:10.050 1993-1993/? I/Radio-JNI: register_android_hardware_Radio DONE 
07-18 09:45:10.059 1993-1993/? D/AndroidRuntime: Calling main entry com.android.commands.pm.Pm 
07-18 09:45:10.064 1993-2001/? W/MessageQueue: Handler (android.os.Handler) {f6bbfb3} sending message to a Handler on a dead thread 
               java.lang.IllegalStateException: Handler (android.os.Handler) {f6bbfb3} sending message to a Handler on a dead thread 
                at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543) 
                at android.os.Handler.enqueueMessage(Handler.java:643) 
                at android.os.Handler.sendMessageAtTime(Handler.java:612) 
                at android.os.Handler.sendMessageDelayed(Handler.java:582) 
                at android.os.Handler.post(Handler.java:338) 
                at android.os.ResultReceiver$MyResultReceiver.send(ResultReceiver.java:57) 
                at com.android.internal.os.IResultReceiver$Stub.onTransact(IResultReceiver.java:58) 
                at android.os.Binder.execTransact(Binder.java:565) 
07-18 09:45:10.064 1993-1993/? I/art: System.exit called, status: 0 
07-18 09:45:10.064 1993-1993/? I/AndroidRuntime: VM exiting with result code 0. 
07-18 09:45:13.487 2005-2005/? D/AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<< 
07-18 09:45:13.491 2005-2005/? D/AndroidRuntime: CheckJNI is OFF 
07-18 09:45:13.569 2005-2005/? W/art: Unexpected CPU variant for X86 using defaults: x86 
07-18 09:45:13.575 2005-2005/? D/ICU: No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat 
07-18 09:45:13.708 2009-2009/? D/AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<< 
07-18 09:45:13.715 2009-2009/? D/AndroidRuntime: CheckJNI is OFF 
07-18 09:45:13.760 2005-2005/? E/memtrack: Couldn't load memtrack module (No such file or directory) 
07-18 09:45:13.760 2005-2005/? E/android.os.Debug: failed to load memtrack module: -2 
07-18 09:45:13.761 2005-2005/? I/Radio-JNI: register_android_hardware_Radio DONE 
07-18 09:45:13.776 2005-2005/? D/AndroidRuntime: Calling main entry com.android.commands.wm.Wm 
07-18 09:45:13.814 2009-2009/? W/art: Unexpected CPU variant for X86 using defaults: x86 
07-18 09:45:13.825 2009-2009/? D/ICU: No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat 
07-18 09:45:13.833 2005-2005/? D/AndroidRuntime: Shutting down VM 
07-18 09:45:13.839 2005-2019/? W/art: Thread attaching while runtime is shutting down: Binder:2005_2 
07-18 09:45:13.839 2005-2019/? I/AndroidRuntime: NOTE: attach of thread 'Binder:2005_2' failed 
07-18 09:45:13.891 2009-2009/? E/memtrack: Couldn't load memtrack module (No such file or directory) 
07-18 09:45:13.891 2009-2009/? E/android.os.Debug: failed to load memtrack module: -2 
07-18 09:45:13.892 2009-2009/? I/Radio-JNI: register_android_hardware_Radio DONE 
07-18 09:45:13.903 2009-2009/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am 
07-18 09:45:13.909 765-1149/system_process I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.prince.fragment_modularui2/.MainActivity} from uid 0 on display 0 
07-18 09:45:13.919 2009-2009/? D/AndroidRuntime: Shutting down VM 
07-18 09:45:13.993 765-1021/system_process W/ActivityManager: Slow operation: 75ms so far, now at startProcess: returned from zygote! 
07-18 09:45:13.993 765-1021/system_process W/ActivityManager: Slow operation: 75ms so far, now at startProcess: done updating battery stats 
07-18 09:45:13.993 765-1021/system_process W/ActivityManager: Slow operation: 75ms so far, now at startProcess: building log message 
07-18 09:45:13.993 765-1021/system_process I/ActivityManager: Start proc 2027:com.example.prince.fragment_modularui2/u0a115 for activity com.example.prince.fragment_modularui2/.MainActivity 
07-18 09:45:13.993 765-1021/system_process W/ActivityManager: Slow operation: 75ms so far, now at startProcess: starting to update pids map 
07-18 09:45:13.993 765-1021/system_process W/ActivityManager: Slow operation: 75ms so far, now at startProcess: done updating pids map 
07-18 09:45:13.993 765-1021/system_process W/ActivityManager: Slow operation: 75ms so far, now at startProcess: done starting proc! 
07-18 09:45:14.003 2027-2027/? I/art: Late-enabling -Xcheck:jni 
07-18 09:45:14.004 2027-2027/? W/art: Unexpected CPU variant for X86 using defaults: x86 
07-18 09:45:14.258 500-757/? D/gralloc_vbox86: gralloc_alloc: format 1 and usage 0x933 imply creation of host color buffer 
07-18 09:45:14.784 2027-2027/com.example.prince.fragment_modularui2 W/System: ClassLoader referenced unknown path: /data/app/com.example.prince.fragment_modularui2-1/lib/x86 
07-18 09:45:14.813 2027-2027/com.example.prince.fragment_modularui2 I/InstantRun: starting instant run server: is main process 
07-18 09:45:14.814 2027-2027/com.example.prince.fragment_modularui2 V/InstantRun: Starting server socket listening for package com.example.prince.fragment_modularui2 on [email protected] 
07-18 09:45:14.814 2027-2027/com.example.prince.fragment_modularui2 V/InstantRun: Started server for package com.example.prince.fragment_modularui2 
07-18 09:45:14.903 2027-2027/com.example.prince.fragment_modularui2 D/AndroidRuntime: Shutting down VM 
07-18 09:45:14.904 2027-2027/com.example.prince.fragment_modularui2 E/AndroidRuntime: FATAL EXCEPTION: main 
                         Process: com.example.prince.fragment_modularui2, PID: 2027 
                         java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.prince.fragment_modularui2/com.example.prince.fragment_modularui2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2567) 
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                          at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                          at android.os.Looper.loop(Looper.java:154) 
                          at android.app.ActivityThread.main(ActivityThread.java:6119) 
                          at java.lang.reflect.Method.invoke(Native Method) 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
                         Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference 
                          at android.content.ContextWrapper.getResources(ContextWrapper.java:86) 
                          at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127) 
                          at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121) 
                          at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:549) 
                          at com.example.prince.fragment_modularui2.MainActivity.<init>(MainActivity.java:12) 
                          at java.lang.Class.newInstance(Native Method) 
                          at android.app.Instrumentation.newActivity(Instrumentation.java:1078) 
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2557) 
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  
                          at android.app.ActivityThread.-wrap12(ActivityThread.java)  
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  
                          at android.os.Handler.dispatchMessage(Handler.java:102)  
                          at android.os.Looper.loop(Looper.java:154)  
                          at android.app.ActivityThread.main(ActivityThread.java:6119)  
                          at java.lang.reflect.Method.invoke(Native Method)  
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  
07-18 09:45:14.907 765-1021/system_process W/ActivityManager: Force finishing activity com.example.prince.fragment_modularui2/.MainActivity 
07-18 09:45:15.031 500-505/? D/gralloc_vbox86: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer 
07-18 09:45:15.058 500-505/? D/gralloc_vbox86: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer 
07-18 09:45:15.115 500-505/? D/gralloc_vbox86: gralloc_alloc: format 1 and usage 0x900 imply creation of host color buffer 
07-18 09:45:15.232 765-1842/system_process I/OpenGLRenderer: Initialized EGL, version 1.4 
07-18 09:45:15.232 765-1842/system_process D/OpenGLRenderer: Swap behavior 1 
07-18 09:45:15.429 765-779/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{b0310c2 u0 com.example.prince.fragment_modularui2/.MainActivity t693 f} 
07-18 09:45:15.431 765-779/system_process I/ActivityManager: Killing 1353:android.process.media/u0a10 (adj 906): empty #17 
07-18 09:45:15.432 765-779/system_process I/WindowManager: Failed to capture screenshot of Token{7756ed3 ActivityRecord{b0310c2 u0 com.example.prince.fragment_modularui2/.MainActivity t693 f}} appWin=Window{7f1e54b u0 Starting com.example.prince.fragment_modularui2} drawState=1 
07-18 09:45:15.448 765-786/system_process I/Choreographer: Skipped 70 frames! The application may be doing too much work on its main thread. 
07-18 09:45:15.452 765-1150/system_process D/ActivityManager: cleanUpApplicationRecord -- 1353 
07-18 09:45:15.456 765-1842/system_process E/EGL_emulation: tid 1842: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH) 
07-18 09:45:15.456 765-1842/system_process W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xc4afa800, error=EGL_BAD_MATCH 
07-18 09:45:16.337 765-780/system_process I/Choreographer: Skipped 52 frames! The application may be doing too much work on its main thread. 
07-18 09:45:25.438 765-779/system_process W/ActivityManager: Activity destroy timeout for ActivityRecord{b0310c2 u0 com.example.prince.fragment_modularui2/.MainActivity t693 f} 
+0

あなたが取得しているスタックトレース/エラーメッセージを投稿してもらえますか? –

+0

卿、私が質問...のStackTraceが底にある....事前に感謝を編集しました – Ashish

答えて

0

スタックトレースから、ContextのヌルでgetResources()と呼んでいることがわかりました。あなたのようなフィールドとして、あなたのリソースを宣言しているので、これは理にかなって:

Resources res = getResources(); 

この活動が作成される前に実行されますが、アクティビティが作成されると、あなただけのgetResources()を呼び出すことができます。だから、解決策は次のようにリソースを宣言することです:

private Resources res; 

そして、あなたの活動でのonCreateあなたが行う方法:

res = getResources(); 

これはまたのonCreateメソッドがあるまで、あなたが変数RESを使用できないことを意味します実行される。

String[] a; 

、その後のonCreateにだけ呼び出します:a = getResources().getStringArray(R.array.desc);

私はあなたを与えることができるもう一つの先端が名前にある一般的にはそのような変数などのリソースを宣言ではなく、あなたの文字列配列として宣言しない方が良いですあなたの変数はより明確になります。 'a'という名前の文字列配列はあまり説明的ではありません。あなたはそれがより明確にするためにdescriptionArrayのようなもの、それを呼び出すことができます。