Android Horizo​​ntalScrollView不能与EditText

时间:2015-10-02 23:40:33

标签: java android android-edittext horizontalscrollview

所以我有一个Android应用程序输出ascii艺术等同于你输入的内容,例如" hello world"到

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

由于它已经格式化并且需要保持格式化,我想要一个水平滚动的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);
}

这就是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>

每次我尝试运行它时,应用程序都会停止(&#34;不幸的是,ASCIIgen已停止。&#34;)我只是不知道我做错了什么。这是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

非常感谢任何帮助,老实说,我不知道我做错了什么。

1 个答案:

答案 0 :(得分:0)

替换

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

EditText mEditText;
HorizontalScrollView mScrollView;

并在init() method

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