2013-01-08 52 views
0

前幾天我問這個問題,但我只是意識到,我包括了錯誤的logcat的錯誤,這已經包含了正確的logcat ...錯誤傳遞的TextView值

我正在寫一個應用程序,將允許用戶輸入值,它們將用於在圖表上繪圖。最近幾天,我一直被困在一個特定的問題上,似乎無法克服它。用戶將輸入2個值,並從這3個值中計算出來並傳遞給內部類以在圖表上繪製。這些值是TextView窗體的值。我知道大部分代碼的工作原理是我一直在測試它,而且我很確定這個錯誤與TextView值有關。以下是用於計算和繪製圖表的代碼。

package com.example.ballanimation; 

import java.math.BigDecimal; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.RectF; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class calculations extends Activity { 

EditText firstVal, secondVal; 
TextView resultA, resultB, resultC; 
Button button3; 
BigDecimal firstNum, secNum; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
// TODO Auto-generated method stub 
super.onCreate(savedInstanceState); 
setContentView(R.layout.calculations_page); 

button3 = (Button) findViewById(R.id.button3); 

firstVal = (EditText) findViewById(R.id.editTXT1); 
secondVal = (EditText) findViewById(R.id.editTXT2); 

resultA = (TextView) findViewById(R.id.result1); 
resultB = (TextView) findViewById(R.id.result2); 
resultC = (TextView) findViewById(R.id.result3); 


button3.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     firstNum = new BigDecimal(firstVal.getText().toString()); 
     secNum = new BigDecimal(secondVal.getText().toString()); 
     resultA.setText(firstNum.add(secNum).toString()); 
     resultB.setText(firstNum.subtract(secNum).toString()); 
     resultC.setText(firstNum.multiply(secNum).toString()); 

    } 
}); 


} 

class TestA1 extends View{ 

public TestA1(Context context) { 
    super(context); 
    // TODO Auto-generated constructor stub 
} 

public void onDraw(Canvas canvas){ 
    super.onDraw(canvas); 

    String TestA = resultA.getText().toString(); 
    String TestB = resultB.getText().toString(); 
    String TestC = resultC.getText().toString(); 

    int A = Integer.parseInt(TestA); 
    int B = Integer.parseInt(TestB); 
    int C = Integer.parseInt(TestC); 

    Paint red = new Paint(); 
    red.setColor(Color.RED); 
    red.setStyle(Paint.Style.STROKE); 

    Paint black = new Paint(); 
    black.setColor(Color.BLACK); 
    black.setStyle(Paint.Style.STROKE); 

    canvas.drawCircle(A, B, C, black); 
    canvas.drawCircle(canvas.getWidth()*1/2, canvas.getHeight()*3/8, canvas.getWidth()*475/1000, black); 
    canvas.drawCircle(canvas.getWidth()*5/8, canvas.getWidth()*5/8, canvas.getWidth()*349/1000, black); 
    canvas.drawCircle(canvas.getWidth()*6/8, canvas.getWidth()*5/8, canvas.getWidth()*228/1000, black); 
    canvas.drawCircle(canvas.getWidth()*7/8, canvas.getWidth()*5/8, canvas.getWidth()*103/1000, black); 

    canvas.drawLine((canvas.getWidth()-canvas.getWidth()) + 10, (canvas.getHeight()*3)/8, canvas.getWidth() - 10, (canvas.getHeight()*3)/8, black); 

    float a, b, c, d, e, f, g, h, i, j, k, l; 

    a = 90; //degrees 
    b = 90; 
    c = 90; 
    d = 50; 
    e = 90; 
    f = 134; 
    g = -90; 
    h = -90; 
    i = -90; 
    j = -50; 
    k = -90; 
    l = -134; 

    RectF arc0 = new RectF(); 
    RectF arc1 = new RectF(); 
    RectF arc2 = new RectF(); 
    RectF arc3 = new RectF(); 
    RectF arc4 = new RectF(); 
    RectF arc5 = new RectF(); 

    /*left The X coordinate of the left side of the rectangle 
    top The Y coordinate of the top of the rectangle 
    right The X coordinate of the right side of the rectangle 
    bottom The Y coordinate of the bottom of the rectangle */ 



    arc0.set(canvas.getWidth()*1/2, canvas.getHeight()*-139/700, canvas.getWidth()*100/69, canvas.getHeight()*3/8); 
    arc1.set(canvas.getWidth()*-6/112, canvas.getHeight()*-80/100, canvas.getWidth()*195/100, canvas.getHeight()*72/192); 
    arc2.set(canvas.getWidth()*7/10, canvas.getHeight()*70/700, canvas.getWidth()*125/100, canvas.getHeight()*3/8); 
    arc3.set(canvas.getWidth()/2, canvas.getHeight()*3/8, canvas.getWidth()*100/69, canvas.getHeight()*91/96); 
    arc4.set(canvas.getWidth()*-8/112, canvas.getHeight()*3/8, canvas.getWidth()*195/100, canvas.getHeight()*150/100); 
    arc5.set(canvas.getWidth()*7/10, canvas.getHeight()*3/8, canvas.getWidth()*125/100, canvas.getHeight()*65/100); 


    /*oval The bounds of oval used to define the shape and size of the arc 
    startAngle Starting angle (in degrees) where the arc begins 
    sweepAngle Sweep angle (in degrees) measured clockwise 
    useCenter If true, include the center of the oval in the arc, and close it if it is being stroked. This will draw a wedge 
    paint The paint used to draw the arc */ 

    canvas.drawArc(arc0, a, b, false, red); 
    canvas.drawArc(arc1, c, d, false, red); 
    canvas.drawArc(arc2, e, f, false, red); 
    canvas.drawArc(arc3, g, h, false, red); 
    canvas.drawArc(arc4, i, j, false, red); 
    canvas.drawArc(arc5, k, l, false, red); 

} 
} 
} 

我得到的logcat的錯誤是:

01-08 11:54:05.436: D/dalvikvm(714): GC_FOR_ALLOC freed 42K, 3% free 8053K/8259K, paused 66ms, total 69ms 
01-08 11:54:05.436: I/dalvikvm-heap(714): Grow heap (frag case) to 8.305MB for 412064-byte allocation 
01-08 11:54:05.596: D/dalvikvm(714): GC_FOR_ALLOC freed 1K, 3% free 8455K/8711K, paused 55ms, total 55ms 
01-08 11:54:05.647: D/dalvikvm(714): GC_FOR_ALLOC freed 0K, 3% free 8455K/8711K, paused 48ms, total 48ms 
01-08 11:54:05.666: I/dalvikvm-heap(714): Grow heap (frag case) to 9.189MB for 928216-byte allocation 
01-08 11:54:05.836: D/dalvikvm(714): GC_CONCURRENT freed 0K, 4% free 9361K/9671K, paused 98ms+9ms, total 173ms 
01-08 11:54:05.836: D/dalvikvm(714): WAIT_FOR_CONCURRENT_GC blocked 3ms 
01-08 11:54:06.186: D/gralloc_goldfish(714): Emulator without GPU emulation detected. 
01-08 11:54:07.856: I/Choreographer(714): Skipped 79 frames! The application may be doing too much work on its main thread. 
01-08 11:54:25.786: D/AndroidRuntime(714): Shutting down VM 
01-08 11:54:25.816: W/dalvikvm(714): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 
01-08 11:54:25.866: E/AndroidRuntime(714): FATAL EXCEPTION: main 
01-08 11:54:25.866: E/AndroidRuntime(714): java.lang.NullPointerException 
01-08 11:54:25.866: E/AndroidRuntime(714): at com.example.ballanimation.calculations$TestA1.onDraw(calculations.java:83) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.View.draw(View.java:13458) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.View.draw(View.java:13342) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.ViewGroup.drawChild(ViewGroup.java:2929) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.View.draw(View.java:13461) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.widget.FrameLayout.draw(FrameLayout.java:467) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.View.draw(View.java:13342) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.ViewGroup.drawChild(ViewGroup.java:2929) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.View.draw(View.java:13340) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.ViewGroup.drawChild(ViewGroup.java:2929) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.View.draw(View.java:13461) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.widget.FrameLayout.draw(FrameLayout.java:467) 
01-08 11:54:25.866: E/AndroidRuntime(714): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2183) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2256) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2151) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2019) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1830) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.Choreographer.doCallbacks(Choreographer.java:555) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.Choreographer.doFrame(Choreographer.java:525) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.os.Handler.handleCallback(Handler.java:615) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.os.Handler.dispatchMessage(Handler.java:92) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.os.Looper.loop(Looper.java:137) 
01-08 11:54:25.866: E/AndroidRuntime(714): at android.app.ActivityThread.main(ActivityThread.java:4745) 
01-08 11:54:25.866: E/AndroidRuntime(714): at java.lang.reflect.Method.invokeNative(Native Method) 
01-08 11:54:25.866: E/AndroidRuntime(714): at java.lang.reflect.Method.invoke(Method.java:511) 
01-08 11:54:25.866: E/AndroidRuntime(714): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
01-08 11:54:25.866: E/AndroidRuntime(714): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
01-08 11:54:25.866: E/AndroidRuntime(714): at dalvik.system.NativeStart.main(Native Method) 
01-08 11:54:28.535: I/Process(714): Sending signal. PID: 714 SIG: 9 

我敢肯定,錯誤被包含在該行的:

    String TestA = resultA.getText().toString(); 
     String TestB = resultB.getText().toString(); 
     String TestC = resultC.getText().toString(); 

一定有什麼錯誤的方式我正在傳遞TextView值,但我無法看到問題出在哪裏。

任何幫助將不勝感激。

+0

'NullPointerException異常01-08 11:54:25.866:E/AndroidRuntime(714):at com.example.ballanimation.calculations $ TestA1.onDraw(computation.java:83)'哪個是你的onDraw()函數中的第83行? –

+0

@你在這行上包含'calculate.java:83'? –

+0

@PratikSharma嗨感謝您的迴應,第83行包含:String TestA = resultA.getText()。toString(); String TestB = resultB.getText()。toString(); String TestC = resultC。的getText()的toString(); – user1927105

回答

0

試試這個:

 String TestA="",TestB="",TestC=""; 

     if(!resultA.getText().toString().equals("")) 
      TestA = resultA.getText().toString(); 
     if(!resultB.getText().toString().equals("")) 
      TestB = resultB.getText().toString(); 
     if(!resultC.getText().toString().equals("")) 
      TestC = resultC.getText().toString(); 

代替:

 String TestA = resultA.getText().toString(); 
     String TestB = resultB.getText().toString(); 
     String TestC = resultC.getText().toString(); 

感謝。

0

您的班級名稱「計算」以小寫字母開頭,並假設它應以大寫字母「C」開頭。

+0

我認爲這應該是一個評論。但我同意你的看法。 –

0
String TestA = resultA.getText().toString(); 
String TestB = resultB.getText().toString(); 
String TestC = resultC.getText().toString(); 

將此代碼放到TestA1類之外並在類中使用此變量。 ,你完成了。它會幫助。

0

嗨這裏幾個原因可能NullPointerException異常

1)首先在文本視圖中的字符串可能沒有進入和。

2)第二個ID中的相應文本視圖中的(R.id.result1, R.id.result2, R.id.result3)可能無法在相同的佈局(R.layout.calculations_page),你已經使用在活動

對於第一個情況下,您可以用PRATIK夏爾馬的意見去目前

爲空第二次檢查resultA, resultB, resultC

if(resultA!=null){ 
}