android程序在虚拟机上运行出错

dongtao1010 2014-04-19 11:53:39
具体错误如下:
我每次更改后存储,都会有错误产生


具体代码如下:
//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文件如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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" >

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

</LinearLayout>

activity_main.xml文件没有修改

查了一晚上的资料,都没有解决,求大神帮忙啊。。。
PS:小小白一个。。。


...全文
134 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongtao1010 2014-04-20
  • 打赏
  • 举报
回复
引用 2 楼 hjywyj 的回复:
删除以下代码: textView = (TextView)findViewById(R.id.textView); textView.setText("Hello Dylan"); 修改fragment的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; } }
不行,代码编译时会报错,说Cannot make a static reference to the non-static field textView 后来我把textView属性改为static变量,显示没有出错。。。感谢啊。。。
dongtao1010 2014-04-20
  • 打赏
  • 举报
回复
引用 1 楼 bios8086 的回复:
你要干什么啊?
不行,代码编译时会报错,说Cannot make a static reference to the non-static field textView 后来我把textView属性改为static变量,显示没有出错。。。感谢啊。。。
  • 打赏
  • 举报
回复
删除以下代码: textView = (TextView)findViewById(R.id.textView); textView.setText("Hello Dylan"); 修改fragment的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; } }
bios8086 2014-04-20
  • 打赏
  • 举报
回复
你要干什么啊?

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧