2016-03-19 5 views
0

私は多言語のアプリケーションを持っています。異なる言語の単語があります。そして私は特定の言葉の言葉を書いたがっています。助けてください、そして私はすぐに爆発に向かいます。Java言語の配列変数で書かれていますか? (Androidスタジオ)

CharSequence Titles[]= 
      { 
      getResources().getString(R.string.titles), 
      getResources().getString(R.string.result), 
      getResources().getString(R.string.contacts) 
      }; 

import android.support.v4.view.ViewPager; 
    import android.os.Bundle; 
    import android.support.v7.app.AppCompatActivity; 
    import android.support.v7.widget.Toolbar; 
    import android.view.Menu; 
    import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity { 

// Declaring Your View and Variables 

Toolbar toolbar; 
ViewPager pager; 
ViewPagerAdapter adapter; 
SlidingTabLayout tabs; 
CharSequence Titles[]= 
     { 
     getResources().getString(R.string.titles), 
     getResources().getString(R.string.result), 
     getResources().getString(R.string.contacts) 
     }; 

int Numboftabs =3; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    // Creating The Toolbar and setting it as the Toolbar for the activity 

    toolbar = (Toolbar) findViewById(R.id.tool_bar); 
    setSupportActionBar(toolbar); 


    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs. 
    adapter = new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs); 

    // Assigning ViewPager View and setting the adapter 
    pager = (ViewPager) findViewById(R.id.pager); 
    pager.setAdapter(adapter); 

    // Assiging the Sliding Tab Layout View 
    tabs = (SlidingTabLayout) findViewById(R.id.tabs); 
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width 

    // Setting Custom Color for the Scroll bar indicator of the Tab View 
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() { 
     @Override 
     public int getIndicatorColor(int position) { 
      return getResources().getColor(R.color.tabsScrollColor); 
     } 
    }); 

    // Setting the ViewPager For the SlidingTabsLayout 
    tabs.setViewPager(pager); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

のGradle発行、このコードのエラーが:

03-19 14:59:42.882 5493-5493/? E/Zygote: v2 
    03-19 14:59:42.882 5493-5493/? I/libpersona: KNOX_SDCARD checking this for 10140 
    03-19 14:59:42.882 5493-5493/? I/libpersona: KNOX_SDCARD not a persona 
    03-19 14:59:42.882 5493-5493/? I/SELinux: Function: selinux_compare_spd_ram , priority [2] , priority version is VE=SEPF_SM-A300F_5.0.2-1_0026 
    03-19 14:59:42.882 5493-5493/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL 
    03-19 14:59:42.882 5493-5493/? I/art: Late-enabling -Xcheck:jni 
    03-19 14:59:42.912 5493-5493/? D/TimaKeyStoreProvider: in addTimaSignatureService 
    03-19 14:59:42.932 5493-5493/? D/TimaKeyStoreProvider: Cannot add TimaSignature Service, License check Failed 
    03-19 14:59:42.932 5493-5493/? D/ActivityThread: Added TimaKesytore provider 
    03-19 14:59:43.042 5493-5493/com.android.example D/AndroidRuntime: Shutting down VM 
    03-19 14:59:43.042 5493-5493/com.android.example E/AndroidRuntime: FATAL EXCEPTION: main                    Process: com.android.example, PID: 5493                   java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.android.example.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:2326)                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2530)                    at android.app.ActivityThread.access$900(ActivityThread.java:163)                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)                     at android.os.Handler.dispatchMessage(Handler.java:102)                      at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5536) 
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:1397) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192) 

    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:85)     at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74) 
    at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:519) 
                          at com.android.example.MainActivity.<init>(MainActivity.java:18) 
    at java.lang.reflect.Constructor.newInstance(Native Method) 
    at java.lang.Class.newInstance(Class.java:1656) 
    at android.app.Instrumentation.newActivity(Instrumentation.java:1071) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2530)  
    at android.app.ActivityThread.access$900(ActivityThread.java:163)  
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)  
    at android.os.Handler.dispatchMessage(Handler.java:102)  
    at android.os.Looper.loop(Looper.java:135)  
    at android.app.ActivityThread.main(ActivityThread.java:5536)  
    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:1397) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1192)  

答えて

0

エラーが言うように、あなたはnull参照エラーがあります。あなたは早すぎる資源を手に入れようとしています。その時点でオブジェクトが準備完了状態にないため、フィールド宣言のメソッドへの呼び出しを追加しないでください。

getString()コールをonCreate()メソッドに移動すると正常に機能します。

+0

ありがとう:)本当に助けてください –

0

問題のコンテキスト値がgetResourcesにヌルである()メソッド

ように変更

Context.getResources() 

または

CharSequence Titles[];   
int Numboftabs =3; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
Titles[]= 
    { 
    getResources().getString(R.string.titles), 
    getResources().getString(R.string.result), 
    getResources().getString(R.string.contacts) 
    }; 

    ---- 

    }