2016-03-19 3 views
0

Ich habe eine mehrsprachige Anwendung. Es gibt Wörter in verschiedenen Sprachen. Und ich möchte das Wort für eine bestimmte Sprache schreiben. Bitte helfen Sie und dann werde ich bald explodieren;Wie in einer Array-Variable in der Sprache von Java geschrieben? (Android Studio)

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

Der Fehler in diesem Code

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 Ausgabe:

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)  

Antwort

0

Wie der Fehler sagt, haben Sie einen Nullreferenzfehler. Sie versuchen, Ressourcen zu früh zu bekommen. Fügen Sie keine Aufrufe zu Methoden in Felddeklarationen hinzu, da das Objekt zu diesem Zeitpunkt noch nicht bereit ist.

Verschieben Sie Ihre getString() Anrufe an die onCreate() Methode und sie werden funktionieren.

+0

Danke :) Sie helfen mir wirklich –

0

Problem ist Kontextwert in GetResources null ist() Methode

so

Context.getResources() 
ändern

oder

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) 
    }; 

    ---- 

    } 
Verwandte Themen