簡體   English   中英

意圖未定義嘗試了很多,沒有任何作用

[英]intent is undefined tried a lot , nothing works

我在Intent中遇到錯誤。 它說: intent is undefined 我一直在搜索很多其他帖子,但它仍然無效。 有任何想法嗎?

為了您的知識,我開發了自己的應用程序與android developpes的基礎。 因為我自己的意圖不起作用,我使用並創建了與示例中相同的類,類DisplayMessageActivity但問題仍然相同,這里是我的一些代碼。

public class Main extends ActionBarActivity {

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();


    }
    }
    catch(Exception e){
          System.out.println("fout zit hier");
    }

}

@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;
    }
    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
     }
} 

嘗試替換Intent構造函數中的第一個參數。

Intent intent = new Intent(this, DisplayMessageActivity.class); // wrong
Intent intent = new Intent(getActivity(), DisplayMessageActivity.class); // right

第一個參數是Context對象,並且記住Fragment不從Context繼承,因此解決方案是從父Activity獲取一個。

暫無
暫無

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

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