綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] 幫忙看一下錯誤訊息

[複製連結] 查看: 1845|回覆: 7|好評: 0
跳轉到指定樓層
樓主
tequila0630 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2014-5-12 00:02

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
這是我的 MainActivity.java 檔案內容

package com.eLeopcard.cxbutton;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
                private Button btnDo;
                private TextView txtShow;
       
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
//*******************
                btnDo = (Button)findViewById(R.id.button1);
                btnDo.setOnClickListener(btnDoListener);
                txtShow = (TextView)findViewById(R.id.textView1);
               
                //**********************
                private Button.OnClickListener btnDoListener =
                                new Button.OnClickListener(){
                        public void onClick(View v){
                                // TODO
                                txtShow.setText("您按到我了!");
                       
                        }
                };
               
                if (savedInstanceState == null) {
                        getSupportFragmentManager().beginTransaction()
                                        .add(R.id.container, new PlaceholderFragment()).commit();
                }
               
       
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

                // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
                // automatically handle clicks on the Home/Up button, so long
                // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();
                if (id == R.id.action_settings) {
                        return true;
                }
                return super.onOptionsItemSelected(item);
        }

        /**
         * A placeholder fragment containing a simple view.
         */
        public static class PlaceholderFragment extends Fragment {

                public PlaceholderFragment() {
                }

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                Bundle savedInstanceState) {
                        View rootView = inflater.inflate(R.layout.fragment_main, container,
                                        false);
                        return rootView;
                }
        }

}

在紅字的地方發生錯誤 請問是哪裡出錯了
「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

板凳
CarolHsieh | 收聽TA | 只看該作者
發表於 2014-5-12 09:28
  1. btnDo = (Button)findViewById(R.id.button1);
  2.             //btnDo.setOnClickListener(btnDoListener);
  3.             txtShow = (TextView)findViewById(R.id.textView1);
  4.             btnDo.setOnClickListener(new OnClickListener() {
  5.                 public void onClick(View v) {
  6.                     // TODO
  7.                     txtShow.setText("您按到我了!");
  8.             
  9.             }
  10.     });
複製代碼


改這樣試試吧

評分

參與人數 1碎鑽 +1 幫助 +1 收起 理由
tequila0630 + 1 + 1 非常讚

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

地板
 樓主| tequila0630 | 收聽TA | 只看該作者
發表於 2014-5-12 10:58
本帖最後由 tequila0630 於 2014-5-12 12:29 編輯

謝謝你的幫忙 可是 在模擬器跟實機上執行都會出現錯誤XD
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

5
CarolHsieh | 收聽TA | 只看該作者
發表於 2014-5-12 13:53
tequila0630 發表於 2014-5-12 10:58
謝謝你的幫忙 可是 在模擬器跟實機上執行都會出現錯誤XD

錯誤是在同樣的地方嗎??
下斷點看看問題在哪
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

6
 樓主| tequila0630 | 收聽TA | 只看該作者
發表於 2014-5-12 16:23
CarolHsieh 發表於 2014-5-12 13:53
錯誤是在同樣的地方嗎??
下斷點看看問題在哪

程式沒有出現錯誤~
是開啟模擬器還有在實機上測試 會出現 "很抱歉,CxButton已停止" 的視窗0.0
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

7
CarolHsieh | 收聽TA | 只看該作者
發表於 2014-5-12 17:04
tequila0630 發表於 2014-5-12 16:23
程式沒有出現錯誤~
是開啟模擬器還有在實機上測試 會出現 "很抱歉,CxButton已停止" 的視窗0.0  ...

如果你的Button和TextView是在Fragment裡
就把你的MainActivity上面宣告Button和TextView
和裡面onCreate部分的Button和TextView都刪掉或先註解掉

再把你的副程式 PlaceholderFragment 修改成這樣試試看

  1. public static class PlaceholderFragment extends Fragment {

  2.                 public PlaceholderFragment() {
  3.                 }

  4.                 @Override
  5.                 public View onCreateView(LayoutInflater inflater, ViewGroup container,
  6.                                 Bundle savedInstanceState) {
  7.                         View rootView = inflater.inflate(R.layout.fragment_main, container,
  8.                                         false);
  9.                         
  10.                         Button btnDo = (Button)rootView.findViewById(R.id.button1);               
  11.                         final TextView txtShow = (TextView)rootView.findViewById(R.id.textView1);
  12.                         btnDo.setOnClickListener(new OnClickListener() {
  13.                             public void onClick(View v) {
  14.                                 // TODO
  15.                                 txtShow.setText("您按到我了!");
  16.                         
  17.                         }
  18.                 });
  19.                         return rootView;
  20.                 }
  21.         }
複製代碼

評分

參與人數 1碎鑽 +1 幫助 +1 收起 理由
tequila0630 + 1 + 1 非常讚

查看全部評分

用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

8
 樓主| tequila0630 | 收聽TA | 只看該作者
發表於 2014-5-12 17:21
CarolHsieh 發表於 2014-5-12 17:04
如果你的Button和TextView是在Fragment裡
就把你的MainActivity上面宣告Button和TextView
和裡面onCreate ...

可以了 謝謝@@!!
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則