Thursday, April 24, 2014

android program to run on a virtual machine error


Specific error is as follows:
Every time I change the store, there will be an error occurs


Specific code as follows:
/ / Main_activity.java

package com.Dylan.s01_e05_view;

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.TextView;

public class MainActivity extends ActionBarActivity {

private TextView textView;

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

textView = (TextView) findViewById (R.id.textView);
textView.setText ("Hello Dylan");
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 {
/ / Private TextView textView;

public PlaceholderFragment () {
}

@ Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate (R.layout.fragment_main, container,
false);
return rootView;
}
}

}


fragment_main.xml following documents
xmlns: tools = "http://schemas.android.com/tools"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: orientation = "vertical"
tools: context = "com.Dylan.s01_e05_view.MainActivity $ PlaceholderFragment">

android: id = "@ + id / textView"
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: background = "# FF0000"
android: text = "Hello world" />



activity_main.xml file is not modified

A night to check the information, are not resolved, ask God to help big ah. . .
PS: small white one. . .


<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
What are you doing ah?
Reply:
Delete the following code:
textView = (TextView) findViewById (R.id.textView);
textView.setText ("Hello Dylan");

Modified fragment of oncreateview
@ Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate (R.layout.fragment_main, container,
false);
TextView textView = (TextView) rootView.findViewById (R.id.textView);
textView.setText ("Hello Dylan");
return rootView;
}
}
Reply:
cited a floor bios8086 reply:
you doing ah?

No, the code compile time error, say Cannot make a static reference to the non-static field textView
Then I put textView property to static variables, shows no error. . . Thank ah. . .
Reply:
reference to the second floor hjywyj reply:
delete the following code:
textView = (TextView) findViewById (R.id.textView);
textView.setText ("Hello Dylan");

Modified fragment of oncreateview
@ Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate (R.layout.fragment_main, container,
false);
TextView textView = (TextView) rootView.findViewById (R.id.textView);
textView.setText ("Hello Dylan");
return rootView;
}
}



No, the code compile time error, say Cannot make a static reference to the non-static field textView
Then I put textView property to static variables, shows no error. . . Thank ah. . .

No comments:

Post a Comment