2014-11-17 92 views
0

我需要一些android編程幫助。所以onbutton點擊我打電話在icite方法。基本上,單擊按鈕時,所有文本框文本都將轉換爲一個字符串到一個新變量上。之後,我將所有變量組合成一個名爲Total的字符串。然後我將標籤textView10的文本更改爲字符串Total。但它只是崩潰我的應用程序。你知道我會做錯什麼嗎? 我logcat的:點擊按鈕後文字不變

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); 
     */ 


    } 
} 
+0

請發佈您的logcat錯誤。 –

+0

你在'citation.xml'佈局中定義'button10'嗎? –

回答

1

你的觀點引起nullpointerexception,因爲他們不R.layout.citations按鈕視圖的一部分。所以,定義如下意見的的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,因此,init它也可以作爲

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); 
     } 
    }); 
+0

仍然出現錯誤。它只是讓我的應用程序崩潰。 –

+0

@RifatRashid你忘了初始化editText2,因此,init它也可以作爲 'editText2 =(EditText上)findViewById(R.id.editText2);'見編輯答案。 –

1

我同意@ Giru-巴伊,組件視圖具有在創建的視圖中定義,在OnCreate()方法,而不是內部的任何其它方法,所以該塊必須僅一次:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.citations); 
    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 Total = authorLast1+","+" "+authorFirst1+"."+" "+"\""+articleTitle1+"."+"\""+" "+website1+"."+" "+Publisher1+","+" "+day2+" "+month2+" "+year2+"."+" "+"Web"+"."+" "+day+" "+month+"."+" "+year+"."; 
     citationText.setText(authorFirst1); 

我相信你設置的標籤CitationText爲authorFirst1,也級聯可以改善的內容有錯誤。

String Total = authorLast1+","+" "+authorFirst1+"."+" "+"\""+articleTitle1+"."+"\""+" "+website1+"."+" "+Publisher1+","+" "+day2+" "+month2+" "+year2+"."+" "+"Web"+"."+" "+day1+" "+month1+"."+" "+year1+"."; 
     citationText.setText(authorFirst1); 

String Total = authorLast1+", "+authorFirst1+". \""+articleTitle1+".\" "+website1+"."+Publisher1+", "+day2+" "+month2+" "+year2+". "+"Web"+". "+day1+" "+month1+". "+year1+"."; 
     citationText.setText(Total); 

如果你不使用數組時,你會得到一個NullPointerException異常的唯一原因是,當你解決一個視圖的代碼無法識別。只要您開始編寫OnCreate()方法,所有視圖都必須聲明爲佈局的一部分。