简体   繁体   English

Android Horizo​​ntalScrollView不适用于EditText

[英]Android HorizontalScrollView doesn't work with EditText

So I have an android app that outputs the ascii art equivalent for what you type in, like "hello world" to 因此,我有一个android应用,可以输出与您输入内容相同的ascii art,例如“ hello world”

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

Being that it's formatted and needs to stay formatted, I want a horizontal scrolling edittext to put it in. I tried to make one but it didn't work very well... Here's the Java: 由于它已经格式化并且需要保持格式化,因此我想放置一个水平滚动的edittext。我尝试制作一个,但是效果不是很好。这是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);
}

And here's the XML: 这是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>

Every time I try to run it, the application stops ("Unfortunately, ASCIIgen has stopped.") I just have no idea what I'm doing wrong. 每当我尝试运行它时,应用程序都会停止运行(“不幸的是,ASCIIgen已停止。”)我只是不知道自己在做什么错。 Here's the logcat. 这是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

Any help is greatly appreciated, I honestly have no idea what I'm doing wrong. 非常感谢您的帮助,老实说,我不知道我在做什么错。

Replace 更换

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

with

EditText mEditText;
HorizontalScrollView mScrollView;

and initialize inside init() method 并在init() method初始化

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM