2015-10-02 1 views
0

J'ai une application Android qui affiche l'art ascii équivalent pour ce que vous tapez, comme « Bonjour tout le monde » àAndroid HorizontalScrollView ne fonctionne pas avec EditText

_   _ _       _  _ 
| |__ ___| | | ___ __  _____ _ __| | __| | 
| '_ \/_ \ | |/ _ \ \ \ /\// _ \| '__| |/ _` | 
| | | | __/ | | (_) | \ V V/(_) | | | | (_| | 
|_| |_|\___|_|_|\___/ \_/\_/ \___/|_| |_|\__,_| 

étant qu'il est formaté et doit rester . formaté, je veux un edittext de défilement horizontal pour le mettre en j'ai essayé de faire un, mais il ne fonctionne pas très bien ... Voici Java:

EditText mEditText = (EditText) findViewById(R.id.TEXT_STATUS_ID); 
HorizontalScrollView mScrollView = (HorizontalScrollView) findViewById(R.id.SCROLLER_ID); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    init(); 
} 
private void loadDoc(){ 
    Intent intent = getIntent(); 
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 
    String letter; 
    EditText textView = new EditText(this); 
    Typeface courier = Typeface.createFromAsset(getAssets(),"Courier_Prime.ttf"); 
    textView.setTextSize(20); 
    textView.setTypeface(courier); 
    setContentView(textView); 
    String value; 
    String toPrint; 
    int textLength = message.length(); 
    for(int i = 0; i <= 14; i++){ 
     for(int j = 0; j < textLength; j++){ 
      letter = message.substring(j,j+1).toUpperCase(Locale.getDefault()); 
      value = letter + i; 
      toPrint = isometric.get(value); 
      textView.append(toPrint); 
      if(j == textLength-1){ 
       textView.append("\n"); 
      } 
     } 
    } 
} 

private void init() { 
    loadDoc(); 
    scrollToBottom(); 
} 

private void scrollToBottom() { 
    mScrollView.post(new Runnable() { 
     public void run() { 
      mEditText.setHorizontallyScrolling(true); 
      mEditText.setMovementMethod(new ScrollingMovementMethod()); 
      mScrollView.smoothScrollTo(0, mEditText.getBottom()); 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.display_message, 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(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

Et voici le XML:

<HorizontalScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/SCROLLER_ID" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="#333333" 
android:fillViewport="true" 
android:scrollbars="horizontal"> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <EditText 
     android:id="@+id/TEXT_STATUS_ID" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center" 
     android:layout_weight="1.0" 
     android:textColor="#cccccc" 
     android:textSize="35sp" /> 
</LinearLayout> 
</HorizontalScrollView> 

Chaque fois que j'essaie de l'exécuter, l'application s'arrête ("Malheureusement, ASCIIgen s'est arrêté.") Je n'ai aucune idée de ce que je fais de mal. Voici le logcat.

10-02 19:31:46.830: D/dalvikvm(864): Not late-enabling CheckJNI (already on) 
10-02 19:31:48.210: I/dalvikvm(864): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted 
10-02 19:31:48.210: W/dalvikvm(864): VFY: unable to resolve virtual method 12212: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V 
10-02 19:31:48.210: D/dalvikvm(864): VFY: replacing opcode 0x6f at 0x0000 
10-02 19:31:48.210: I/dalvikvm(864): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll 
10-02 19:31:48.210: W/dalvikvm(864): VFY: unable to resolve virtual method 12218: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V 
10-02 19:31:48.210: D/dalvikvm(864): VFY: replacing opcode 0x6f at 0x0000 
10-02 19:31:48.230: I/dalvikvm(864): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled 
10-02 19:31:48.230: W/dalvikvm(864): VFY: unable to resolve virtual method 9779: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll()V 
10-02 19:31:48.230: D/dalvikvm(864): VFY: replacing opcode 0x6e at 0x000e 
10-02 19:31:48.340: I/dalvikvm(864): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations 
10-02 19:31:48.340: W/dalvikvm(864): VFY: unable to resolve virtual method 393: Landroid/content/res/TypedArray;.getChangingConfigurations()I 
10-02 19:31:48.340: D/dalvikvm(864): VFY: replacing opcode 0x6e at 0x0002 
10-02 19:31:48.340: I/dalvikvm(864): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType 
10-02 19:31:48.340: W/dalvikvm(864): VFY: unable to resolve virtual method 415: Landroid/content/res/TypedArray;.getType (I)I 
10-02 19:31:48.340: D/dalvikvm(864): VFY: replacing opcode 0x6e at 0x0002 
10-02 19:31:48.370: I/dalvikvm(864): Could not find method android.content.res.Resources.getDrawable, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawable 
10-02 19:31:48.370: W/dalvikvm(864): VFY: unable to resolve virtual method 356: Landroid/content/res/Resources;.getDrawable (ILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 
10-02 19:31:48.370: D/dalvikvm(864): VFY: replacing opcode 0x6e at 0x0002 
10-02 19:31:48.370: I/dalvikvm(864): Could not find method android.content.res.Resources.getDrawableForDensity, referenced from method android.support.v7.internal.widget.ResourcesWrapper.getDrawableForDensity 
10-02 19:31:48.370: W/dalvikvm(864): VFY: unable to resolve virtual method 358: Landroid/content/res/Resources;.getDrawableForDensity (IILandroid/content/res/Resources$Theme;)Landroid/graphics/drawable/Drawable; 
10-02 19:31:48.370: D/dalvikvm(864): VFY: replacing opcode 0x6e at 0x0002 
10-02 19:31:49.120: I/Choreographer(864): Skipped 34 frames! The application may be doing too much work on its main thread. 
10-02 19:31:49.140: D/gralloc_goldfish(864): Emulator without GPU emulation detected. 
10-02 19:31:52.230: I/Choreographer(864): Skipped 64 frames! The application may be doing too much work on its main thread. 
10-02 19:31:53.700: I/Choreographer(864): Skipped 98 frames! The application may be doing too much work on its main thread. 
10-02 19:31:54.410: D/AndroidRuntime(864): Shutting down VM 
10-02 19:31:54.410: W/dalvikvm(864): threadid=1: thread exiting with uncaught exception (group=0xb2a12ba8) 
10-02 19:31:54.460: E/AndroidRuntime(864): FATAL EXCEPTION: main 
10-02 19:31:54.460: E/AndroidRuntime(864): Process: com.dumpong.asciigen, PID: 864 
10-02 19:31:54.460: E/AndroidRuntime(864): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dumpong.asciigen/com.dumpong.myfirstapp.DisplayMessageActivity}: java.lang.NullPointerException 
10-02 19:31:54.460: E/AndroidRuntime(864): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121) 
10-02 19:31:54.460: E/AndroidRuntime(864): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
10-02 19:31:54.460: E/AndroidRuntime(864): at android.app.ActivityThread.access$800(ActivityThread.java:135) 
10-02 19:31:54.460: E/AndroidRuntime(864): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
10-02 19:31:54.460: E/AndroidRuntime(864): at android.os.Handler.dispatchMessage(Handler.java:102) 
10-02 19:31:54.460: E/AndroidRuntime(864): at android.os.Looper.loop(Looper.java:136) 
10-02 19:31:54.460: E/AndroidRuntime(864): at android.app.ActivityThread.main(ActivityThread.java:5017) 
10-02 19:31:54.460: E/AndroidRuntime(864): at java.lang.reflect.Method.invokeNative(Native Method) 
10-02 19:31:54.460: E/AndroidRuntime(864): at java.lang.reflect.Method.invoke(Method.java:515) 
10-02 19:31:54.460: E/AndroidRuntime(864): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
10-02 19:31:54.460: E/AndroidRuntime(864): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
10-02 19:31:54.460: E/AndroidRuntime(864): at dalvik.system.NativeStart.main(Native Method) 
10-02 19:31:54.460: E/AndroidRuntime(864): Caused by: java.lang.NullPointerException 
10-02 19:31:54.460: E/AndroidRuntime(864): at android.app.Activity.findViewById(Activity.java:1884) 
10-02 19:31:54.460: E/AndroidRuntime(864): at com.dumpong.myfirstapp.DisplayMessageActivity.<init>(DisplayMessageActivity.java:295) 
10-02 19:31:54.460: E/AndroidRuntime(864): at java.lang.Class.newInstanceImpl(Native Method) 
10-02 19:31:54.460: E/AndroidRuntime(864): at java.lang.Class.newInstance(Class.java:1208) 
10-02 19:31:54.460: E/AndroidRuntime(864): at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 
10-02 19:31:54.460: E/AndroidRuntime(864): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112) 
10-02 19:31:54.460: E/AndroidRuntime(864): ... 11 more 
10-02 19:31:58.730: I/Process(864): Sending signal. PID: 864 SIG: 9 

Toute aide est grandement appréciée, je n'ai honnêtement aucune idée de ce que je fais mal.

Répondre

0

Remplacer

EditText mEditText = (EditText) findViewById(R.id.TEXT_STATUS_ID); 
HorizontalScrollView mScrollView = (HorizontalScrollView) findViewById(R.id.SCROLLER_ID); 

avec

EditText mEditText; 
HorizontalScrollView mScrollView; 

et initialize l'intérieur init() method

private void init() { 
mEditText = (EditText) findViewById(R.id.TEXT_STATUS_ID); 
mScrollView = (HorizontalScrollView) findViewById(R.id.SCROLLER_ID); 
loadDoc(); 
scrollToBottom(); 
} 
+0

Je l'ai essayé. Pour une raison quelconque, cela n'a pas fonctionné ... – dumpong

+0

Pourquoi avez-vous textView dans 'setContentView (textView);' au lieu de passer votre mise en page xml. – Tauqir

+0

Vous n'avez utilisé aucune référence de votre mise en page XML. – Tauqir