当前位置: 首页 > 知识库问答 >
问题:

单击按钮后文本不变

连时铭
2023-03-14

我需要一些android编程的帮助。所以onbutton click我在icite方法中调用。基本上,当单击按钮时,所有文本框文本都将转换为一个新变量上的字符串。之后,我将所有变量组合成一个名为total的字符串。然后将标签textView10的文本更改为字符串total。然而,它只是崩溃了我的应用程序。你知道我可能做错了什么吗?我的猫:

11-17 03:02:43.652: W/dalvikvm(1952): threadid=1: thread exiting with uncaught exception (group=0xb1f60648)
11-17 03:02:43.662: E/AndroidRuntime(1952): FATAL EXCEPTION: main
11-17 03:02:43.662: E/AndroidRuntime(1952): java.lang.NullPointerException
11-17 03:02:43.662: E/AndroidRuntime(1952):     at com.example.randomapplication.citationsPage$1.onClick(citationsPage.java:59)
11-17 03:02:43.662: E/AndroidRuntime(1952):     at android.view.View.performClick(View.java:4240)
11-17 03:02:43.662: E/AndroidRuntime(1952):     at android.view.View$PerformClick.run(View.java:17721)
11-17 03:02:43.662: E/AndroidRuntime(1952):     at android.os.Handler.handleCallback(Handler.java:730)
11-17 03:02:43.662: E/AndroidRuntime(1952):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-17 03:02:43.662: E/AndroidRuntime(1952):     at android.os.Looper.loop(Looper.java:137)
11-17 03:02:43.662: E/AndroidRuntime(1952):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-17 03:02:43.662: E/AndroidRuntime(1952):     at java.lang.reflect.Method.invokeNative(Native Method)
11-17 03:02:43.662: E/AndroidRuntime(1952):     at java.lang.reflect.Method.invoke(Method.java:525)
11-17 03:02:43.662: E/AndroidRuntime(1952):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-17 03:02:43.662: E/AndroidRuntime(1952):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-17 03:02:43.662: E/AndroidRuntime(1952):     at dalvik.system.NativeStart.main(Native Method)
11-17 03:02:45.552: D/dalvikvm(1982): GC_FOR_ALLOC freed 92K, 11% free 2541K/2832K, paused 8ms, total 9ms
11-17 03:02:45.552: I/dalvikvm-heap(1982): Grow heap (frag case) to 3.277MB for 635808-byte allocation
11-17 03:02:45.562: D/dalvikvm(1982): GC_FOR_ALLOC freed <1K, 9% free 3162K/3456K, paused 11ms, total 11ms
11-17 03:02:45.572: D/dalvikvm(1982): GC_FOR_ALLOC freed <1K, 9% free 3162K/3456K, paused 4ms, total 4ms
11-17 03:02:45.572: I/dalvikvm-heap(1982): Grow heap (frag case) to 3.753MB for 500412-byte allocation
11-17 03:02:45.592: D/dalvikvm(1982): GC_FOR_ALLOC freed <1K, 8% free 3650K/3948K, paused 14ms, total 15ms
11-17 03:02:45.662: D/libEGL(1982): loaded /system/lib/egl/libEGL_emulation.so
11-17 03:02:45.662: D/(1982): HostConnection::get() New Host Connection established 0xb7acdfa0, tid 1982
11-17 03:02:45.672: D/libEGL(1982): loaded /system/lib/egl/libGLESv1_CM_emulation.so
11-17 03:02:45.682: D/libEGL(1982): loaded /system/lib/egl/libGLESv2_emulation.so
11-17 03:02:45.692: W/EGL_emulation(1982): eglSurfaceAttrib not implemented
11-17 03:02:45.702: D/OpenGLRenderer(1982): Enabling debug mode 0

代码

package com.example.randomapplication;

import java.util.Calendar;


    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.ClipData;
    import android.content.ClipboardManager;
    import android.content.Context;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;

    public class citationsPage extends Activity {
    EditText editText1;
    EditText author;
    EditText articleTitle;
    EditText webTitle;
    EditText editText2;
    EditText webDay;
    EditText webMonth;
    EditText webYears;
    TextView textView10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.citations);
        final Button citationButton = (Button) findViewById(R.id.button10);
        citationButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Calendar cal = Calendar.getInstance();
                // Gets date from the calendar!
                int day = cal.get(Calendar.DAY_OF_MONTH);
                int month = cal.get(Calendar.MONTH);
                int year = cal.get(Calendar.YEAR);

                String month1 = String.valueOf(month);
                String day1 = String.valueOf(day);
                String year1 = String.valueOf(year);

                editText1 = (EditText) findViewById(R.id.editText1);
                author = (EditText) findViewById(R.id.author);
                articleTitle = (EditText) findViewById(R.id.articleTitle);
                webTitle = (EditText) findViewById(R.id.webTitle);
                webDay = (EditText) findViewById(R.id.webDay);
                webMonth = (EditText) findViewById(R.id.webMonth);
                webYears = (EditText) findViewById(R.id.webYears);


                String authorFirst1 = editText1.getText().toString();
                String authorLast1 = author.getText().toString();
                String articleTitle1 = articleTitle.getText().toString();
                String website1 = webTitle.getText().toString();
                String Publisher1 = editText2.getText().toString();
                String month2 = webMonth.getText().toString();
                String day2 = webDay.getText().toString();
                String year2 = webYears.getText().toString();
                TextView citationText = (TextView) findViewById(R.id.textView10);
                String Total = authorLast1+","+" "+authorFirst1+"."+" "+"\""+articleTitle1+"."+"\""+" "+website1+"."+" "+Publisher1+","+" "+day2+" "+month2+" "+year2+"."+" "+"Web"+"."+" "+day+" "+month+"."+" "+year+".";
                citationText.setText(authorFirst1);



            }
        });

    }

    @SuppressLint("NewApi") public void iCite() {


        Calendar cal = Calendar.getInstance();
        // Gets date from the calendar!
        int day = cal.get(Calendar.DAY_OF_MONTH);
        int month = cal.get(Calendar.MONTH);
        int year = cal.get(Calendar.YEAR);

        String month1 = String.valueOf(month);
        String day1 = String.valueOf(day);
        String year1 = String.valueOf(year);

        editText1 = (EditText) findViewById(R.id.editText1);
        author = (EditText) findViewById(R.id.author);
        articleTitle = (EditText) findViewById(R.id.articleTitle);
        webTitle = (EditText) findViewById(R.id.webTitle);
        webDay = (EditText) findViewById(R.id.webDay);
        webMonth = (EditText) findViewById(R.id.webMonth);
        webYears = (EditText) findViewById(R.id.webYears);


        String authorFirst1 = editText1.getText().toString();
        String authorLast1 = author.getText().toString();
        String articleTitle1 = articleTitle.getText().toString();
        String website1 = webTitle.getText().toString();
        String Publisher1 = editText2.getText().toString();
        String month2 = webMonth.getText().toString();
        String day2 = webDay.getText().toString();
        String year2 = webYears.getText().toString();
        TextView citationText = (TextView) findViewById(R.id.textView10);
        String Total = authorLast1+","+" "+authorFirst1+"."+" "+"\""+articleTitle1+"."+"\""+" "+website1+"."+" "+Publisher1+","+" "+day2+" "+month2+" "+year2+"."+" "+"Web"+"."+" "+day+" "+month+"."+" "+year+".";
        citationText.setText(Total);

        /*ClipboardManager clipboard = (ClipboardManager)
        getSystemService(Context.CLIPBOARD_SERVICE);
        Uri copyUri = Uri.parse(authorLast1+","+" "+authorFirst1+"."+" "+"\""+articleTitle1+"."+"\""+" "+website1+"."+" "+Publisher1+","+" "+day2+" "+month2+" "+year2+"."+" "+"Web"+"."+" "+day+" "+month+"."+" "+year+".");
        //ClipData clip = ClipData.newPlainText(Total + "");
        ClipData clip = ClipData.newUri(getContentResolver(),"URI",copyUri);
        */


    }
}

共有1个答案

容飞掣
2023-03-14

您的视图导致nullpointerexception,因为它们是r.layout.citions而不是按钮视图的一部分。因此,在onclick之外定义以下视图

        editText1 = (EditText) findViewById(R.id.editText1);
        author = (EditText) findViewById(R.id.author);
        articleTitle = (EditText) findViewById(R.id.articleTitle);
        webTitle = (EditText) findViewById(R.id.webTitle);
        webDay = (EditText) findViewById(R.id.webDay);
        webMonth = (EditText) findViewById(R.id.webMonth);
        webYears = (EditText) findViewById(R.id.webYears);
        TextView citationText = (TextView) findViewById(R.id.textView10);

另外,您忘记初始化editText2,所以也将其初始化为

  editText2 = (EditText) findViewById(R.id.editText2);

即将onCreate方法重写为

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.citations);
    final Button citationButton = (Button) findViewById(R.id.button10);

    editText1 = (EditText) findViewById(R.id.editText1);
    editText2 = (EditText) findViewById(R.id.editText2);
    author = (EditText) findViewById(R.id.author);
    articleTitle = (EditText) findViewById(R.id.articleTitle);
    webTitle = (EditText) findViewById(R.id.webTitle);
    webDay = (EditText) findViewById(R.id.webDay);
    webMonth = (EditText) findViewById(R.id.webMonth);
    webYears = (EditText) findViewById(R.id.webYears);
    final  TextView citationText = (TextView) findViewById(R.id.textView10);


    citationButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Calendar cal = Calendar.getInstance();
            // Gets date from the calendar!
            int day = cal.get(Calendar.DAY_OF_MONTH);
            int month = cal.get(Calendar.MONTH);
            int year = cal.get(Calendar.YEAR);

            String month1 = String.valueOf(month);
            String day1 = String.valueOf(day);
            String year1 = String.valueOf(year);

            String authorFirst1 = editText1.getText().toString();
            String authorLast1 = author.getText().toString();
            String articleTitle1 = articleTitle.getText().toString();
            String website1 = webTitle.getText().toString();
            String Publisher1 = editText2.getText().toString();
            String month2 = webMonth.getText().toString();
            String day2 = webDay.getText().toString();
            String year2 = webYears.getText().toString();
            String Total = authorLast1+","+" "+authorFirst1+"."+" "+"\""+articleTitle1+"."+"\""+" "+website1+"."+" "+Publisher1+","+" "+day2+" "+month2+" "+year2+"."+" "+"Web"+"."+" "+day+" "+month+"."+" "+year+".";
            citationText.setText(authorFirst1);
        }
    });
 类似资料:
  • 编辑:底部的解决方案 这是一个跳棋游戏。单击一个按钮后,它等待单击第二个按钮与之交换。然而,有时你可能不想移动那个按钮,但一旦你点击了它,就没有回头路了,因为我无法禁用它。 在这里的其他帖子中,我看到人们使用 这只是使它在第一次单击后不可见。 这什么都干不了。 这也没什么用。编辑:所有这些方法都用true和false进行了尝试。 私有无效交换(){ 但你也需要 这样它就重新启用了它,或者其他什么,

  • 有没有可能用硒点击具有相同文本的乘法按钮?

  • 问题内容: 我正在尝试创建使用此lambda函数单击的按钮的列表: 似乎有点工作,但它会立即打印按钮文本,即它不等待用户单击按钮。 关于如何使其对按钮单击做出响应的任何想法? 问题答案: 试图在lambda中完成所有这些操作是错误的方法。简直太令人困惑了,即使不是不可能做您想做的事情。而是创建一个可以完成工作的方法,并仅将lambda用作调用该函数的一种方式:

  • 我已经检查了这个链接,但它提到了长点击。但是我在点击自定义对话框的按钮后面临这个问题。我已经把我的代码贴在这里了。有人能帮我避免这个错误吗?

  • 我需要在商品搜索中键入“香蕉”,然后点击“GO”按钮。 在堆栈溢出的帮助下,我可以调出火狐,输入“香蕉”...但是“Go”按钮(基于检查的Go3)不会开火!! 我试过element.click(),试过ActionChains,试过将光标移动到元素,试过验证它已经启用。它只是不会转到下一个搜索页面。 我没有得到任何错误...它只是没有进入下一页。 谢谢你能提供的任何帮助。快把我逼疯了!

  • 当我单击一个按钮时,有没有一种方法可以更改TreeView的TreeItem的文本?我尝试执行oracle示例http://docs.oracle.com/javafx/2/uicontrols/tree-view.htm中所示的操作,但我不想通过单击TreeItem更改它,而是单击按钮。第二步,我想使用上下文菜单打开一个带有Textfield的窗口,在这里我可以手动插入文本以更改treeitem