簡體   English   中英

ADT錯誤代碼

[英]ADT error codes

我正在ADT中創建一些APP,並且正在關注一個教程,但是我被困在執行按鈕以將文本更改為其他內容上。 問題在於,在本教程中,他使用了較舊的ADT版本,而我的則是較新的版本,因此不再起作用。 代碼是這樣的:

(包裝名稱等)

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

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment())
                    .commit();
            final TextView textOne = (TextView) findViewById(R.id.textik);
            Button pushthis = (Button) findViewById(R.id.gombik);}
    }
    @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;
        }
    }

}

這可能是一個更好的例子。 http://www.vogella.com/tutorials/AndroidFragments/article.html https://developer.android.com/training/basics/fragments/creating.html

我認為您的代碼有誤的主要想法是

final TextView textOne = (TextView) findViewById(R.id.textik);
Button pushthis = (Button) findViewById(R.id.gombik);

它們不應在此處初始化和聲明。 如果使用碎片,則應將其移至碎片。 以下是一個簡單的示例。

    public static class PlaceholderFragment extends Fragment implements Button.OnClickListener{
    TextView textOne;
    Button pushthis;
        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            textOne = (TextView) rootView.findViewById(R.id.textik);
            pushthis = (Button) rootView.findViewById(R.id.gombik);
            //set handlers and anything else
            pushthis.setOnClickListener(this);

            return rootView;
        }
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.gombik:
                   textOne.setText("HI");
                   break;
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM