简单应用程序启动时崩溃,编程错误

时间:2018-01-31 09:30:32

标签: android

一个简单的Android程序,用于输入文本(在Edittext中)并复制到clipboard(在点击复制按钮后)...

打开时崩溃,我缺少什么?

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public abstract class MainActivity extends AppCompatActivity implements View.OnClickListener {

    EditText e1;
    Button b1;

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

        e1 = (EditText) findViewById(R.id.editText);
        b1 = (Button) findViewById(R.id.button);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                copytext(view);
            }
        });
    }

        public void copytext(View view) {
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("Copied Text", (CharSequence) e1);
        clipboard.setPrimaryClip(clip);
        }

}

我也试过使用以下但没有成功:

ClipData clip = ClipData.newPlainText("Copied Text", String.valueOf(e1));

我的xml,

<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.quickclip.panky.quickclip.MainActivity">


    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:ems="100"
        android:inputType="textAutoCorrect"
        android:text="Your Voice Input" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText"
        android:layout_centerHorizontal="true"
        android:onClick="copytext"
        android:text="Copy" />

</android.widget.RelativeLayout>

这是log cat

01-31 15:16:19.168 7127-7127/? I/art: Late-enabling -Xcheck:jni
01-31 15:16:19.458 7127-7127/? W/System: ClassLoader referenced unknown path: /data/app/com.quickclip.panky.quickclip-2/lib/arm64
01-31 15:16:19.459 7127-7127/? D/ActivityThread: installProvider: context.getPackageName()=com.quickclip.panky.quickclip
01-31 15:16:19.463 7127-7127/? I/InstantRun: starting instant run server: is main process
01-31 15:16:19.474 7127-7127/? D/AndroidRuntime: Shutting down VM
01-31 15:16:19.475 7127-7127/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.quickclip.panky.quickclip, PID: 7127
                                                 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.quickclip.panky.quickclip/com.quickclip.panky.quickclip.MainActivity}: java.lang.InstantiationException: java.lang.Class<com.quickclip.panky.quickclip.MainActivity> cannot be instantiated
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2536)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2699)
                                                     at android.app.ActivityThread.access$1000(ActivityThread.java:196)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1534)
                                                     at android.os.Handler.dispatchMessage(Handler.java:111)
                                                     at android.os.Looper.loop(Looper.java:207)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5857)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1026)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:887)
                                                  Caused by: java.lang.InstantiationException: java.lang.Class<com.quickclip.panky.quickclip.MainActivity> cannot be instantiated
                                                     at java.lang.Class.newInstance(Native Method)
                                                     at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2526)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2699) 
                                                     at android.app.ActivityThread.access$1000(ActivityThread.java:196) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1534) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                     at android.os.Looper.loop(Looper.java:207) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5857) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1026) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:887) 
01-31 15:16:19.515 7127-7127/? I/Process: Sending signal. PID: 7127 SIG: 9

我是android编程的新手:)

3 个答案:

答案 0 :(得分:2)

阅读newPlainText

<强>结构

ClipData newPlainText (CharSequence label,CharSequence text)

第二个参数 CharSequence: The actual text in the clip.

<强>最后

ClipData clip = ClipData.newPlainText("Copied Text", e1.getText().toString());

<强> FYI

  • 删除 abstract kEYWORD。

onCreate部分

b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               copyMethod();
            }
        });
    }

 public void copytext(View view) 
    {
     copyMethod();
    }  

    public void copyMethod() 
    {
     ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
     ClipData clip = ClipData.newPlainText("Copied Text", (CharSequence) e1.getText().toString());
     clipboard.setPrimaryClip(clip);
    }

答案 1 :(得分:2)

没有任何日志,也可能有其他错误,但这里有一个:

您无法将EditText(一个UI对象)强制转换为CharSequence。相反,这样做:

public void copytext(View view) {
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = ClipData.newPlainText("Copied Text", e1.getText().toString());
    clipboard.setPrimaryClip(clip);
}

答案 2 :(得分:2)

只需从活动中删除抽象,它就会导致java.lang.InstantiationException:

替换

public abstract class MainActivity extends AppCompatActivity implements View.OnClickListener

并实施

public class MainActivity extends AppCompatActivity implements View.OnClickListener

通过覆盖onclick方法实现接口

@Override
    public void onClick(View v) {

    }