无编译错误但应用程序在运行时终止

时间:2013-07-01 09:21:13

标签: android

我有一些问题。

当模拟 print_row(int character,int weight,String code)开始时,我看到:“aplication已意外停止”。

我不明白为什么! 编译时没有任何错误。

请查看以下代码中的评论......

public class MainActivity extends Activity {

private static final String TAG = "MyActivity";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btnCoder = (Button) findViewById(R.id.btnCoder);
    btnDecoder = (Button) findViewById(R.id.btnDecoder);
    IncomeStr = (EditText) findViewById(R.id.IncomeStr);
    tvResult = (TextView) findViewById(R.id.tvResult);

....
}

public void print_row(int character, int weight, String code) {  
Log.d(TAG,(char)character +"  "+ weight +"  "+ code);   // it's work!
Table  = (TableLayout) findViewById(R.id.table);       //on this row terminate...

    TextView Lecter = new TextView(getApplicationContext());
    Lecter.setGravity(Gravity.CENTER_HORIZONTAL);
    Lecter.setTextColor(0xff000000);
    Lecter.setText("222");

    TextView l_Count = new TextView(getApplicationContext());
    l_Count.setGravity(Gravity.CENTER_HORIZONTAL);
    l_Count.setTextColor(0xff000000);
    l_Count.setText("222");

    TextView l_Code = new TextView(getApplicationContext());
    l_Code.setGravity(Gravity.CENTER_HORIZONTAL);
    l_Code.setTextColor(0xff000000);
    l_Code.setText("222");

    TableRow row = new TableRow(getApplicationContext());
    row.addView(Lecter);
    row.addView(l_Count); 
    row.addView(l_Code); 

    Table.addView(row);

}

}

它是一个活动应用程序,因此它有1个布局。如果我在create()上做 - 一切都很好

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Table  = (TableLayout) findViewById(R.id.table);

    TextView Lecter = new TextView(getApplicationContext());
    Lecter.setGravity(Gravity.CENTER_HORIZONTAL);
    Lecter.setTextColor(0xff000000);
    Lecter.setText("222");

    TextView l_Count = new TextView(getApplicationContext());
    l_Count.setGravity(Gravity.CENTER_HORIZONTAL);
    l_Count.setTextColor(0xff000000);
    l_Count.setText("222");

    TextView l_Code = new TextView(getApplicationContext());
    l_Code.setGravity(Gravity.CENTER_HORIZONTAL);
    l_Code.setTextColor(0xff000000);
    l_Code.setText("222");

    TableRow row = new TableRow(getApplicationContext());
    row.addView(Lecter);
    row.addView(l_Count); 
    row.addView(l_Code); 

    Table.addView(row);

}

它是一个引起函数的类......

public class Tree {
ListView lvData;

   public Tree() {}
   public Tree(int character, int weight, boolean leaf) {
     this.leaf = leaf;
     this.character = character;
     this.weight = weight;
   }

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
public void traverse(String code, Huffman h) throws IOException {
    if (leaf) {
         MainActivity m = new MainActivity();
         m.print_row((char)character, weight, code);
      }

      if ( child0 != null) child0.traverse(code + "0", h);
      if ( child1 != null) child1.traverse(code + "1", h);
   }
}

和我的布局......

<TableLayout android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:stretchColumns="*"
                 android:id="@+id/table">
        <TableRow android:id="@+id/header"
                  android:padding="5dp"
                  android:background="#ccc">
            <TextView android:text="Char"
                      android:layout_gravity="center"
                    />
            <TextView android:text="Count"
                      android:layout_gravity="center"
                    />
            <TextView android:text="Bit - kod"
                      android:layout_gravity="center"
                    />
        </TableRow>
    </TableLayout>

LogCat内容:

    07-01 11:26:12.822: D/MyActivity(466): b  2  000
07-01 11:26:12.822: D/MyActivity(466): b  2  000
07-01 11:26:12.822: D/AndroidRuntime(466): Shutting down VM
07-01 11:26:12.822: W/dalvikvm(466): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-01 11:26:12.841: E/AndroidRuntime(466): FATAL EXCEPTION: main
07-01 11:26:12.841: E/AndroidRuntime(466): java.lang.NullPointerException
07-01 11:26:12.841: E/AndroidRuntime(466):  at android.app.Activity.findViewById(Activity.java:1647)
07-01 11:26:12.841: E/AndroidRuntime(466):  at com.example.m_huffman.MainActivity.print_row(MainActivity.java:77)
07-01 11:26:12.841: E/AndroidRuntime(466):  at com.example.m_huffman.Tree.traverse(Tree.java:36)
07-01 11:26:12.841: E/AndroidRuntime(466):  at com.example.m_huffman.Tree.traverse(Tree.java:39)
07-01 11:26:12.841: E/AndroidRuntime(466):  at com.example.m_huffman.Tree.traverse(Tree.java:39)
07-01 11:26:12.841: E/AndroidRuntime(466):  at com.example.m_huffman.Tree.traverse(Tree.java:39)
07-01 11:26:12.841: E/AndroidRuntime(466):  at com.example.m_huffman.Huffman.makeCode(Huffman.java:41)
07-01 11:26:12.841: E/AndroidRuntime(466):  at com.example.m_huffman.MainActivity$1.onClick(MainActivity.java:45)
07-01 11:26:12.841: E/AndroidRuntime(466):  at android.view.View.performClick(View.java:2485)
07-01 11:26:12.841: E/AndroidRuntime(466):  at android.view.View$PerformClick.run(View.java:9080)
07-01 11:26:12.841: E/AndroidRuntime(466):  at android.os.Handler.handleCallback(Handler.java:587)
07-01 11:26:12.841: E/AndroidRuntime(466):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-01 11:26:12.841: E/AndroidRuntime(466):  at android.os.Looper.loop(Looper.java:123)
07-01 11:26:12.841: E/AndroidRuntime(466):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-01 11:26:12.841: E/AndroidRuntime(466):  at java.lang.reflect.Method.invokeNative(Native Method)
07-01 11:26:12.841: E/AndroidRuntime(466):  at java.lang.reflect.Method.invoke(Method.java:507)
07-01 11:26:12.841: E/AndroidRuntime(466):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-01 11:26:12.841: E/AndroidRuntime(466):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-01 11:26:12.841: E/AndroidRuntime(466):  at dalvik.system.NativeStart.main(Native Method)
07-01 11:26:12.852: W/ActivityManager(70):   Force finishing activity com.example.m_huffman/.MainActivity
07-01 11:26:13.412: W/ActivityManager(70): Activity pause timeout for HistoryRecord{406139d0 com.example.m_huffman/.MainActivity}
07-01 11:26:14.541: I/Process(466): Sending signal. PID: 466 SIG: 9
07-01 11:26:14.551: I/WindowManager(70): WIN DEATH: Window{406bd110 com.example.m_huffman/com.example.m_huffman.MainActivity paused=false}
07-01 11:26:14.561: I/ActivityManager(70): Process com.example.m_huffman (pid 466) has died.
07-01 11:26:14.691: W/InputManagerService(70): Got RemoteException sending setActive(false) notification to pid 466 uid 10054

感谢。

0 个答案:

没有答案