2017-04-15 73 views
-4

我的值存储在来自各种活动的静态字符串中。我想这样做,以显示这些:将静态字符串值传递给文本视图

TextView t = (TextView) findViewById(R.id.text_view); 
t.setText(static_String); 

当我做另一活动,它的工作完全一样的东西。现在放置textview不显示任何内容。

下面是代码:

import static com.miniproject32.writeup.S12.scene0; 
import static com.miniproject32.writeup.S16.scene1a; 
import static com.miniproject32.writeup.S16.scene1b; 
import static com.miniproject32.writeup.S16.scene1c; 
import static com.miniproject32.writeup.S16.scene1d; 
import static com.miniproject32.writeup.S16.scene1e; 
import static com.miniproject32.writeup.S19.resolution; 
import static com.miniproject32.writeup.S19.scene2a; 
import static com.miniproject32.writeup.S19.scene2b; 
import static com.miniproject32.writeup.S19.scene2c; 
import static com.miniproject32.writeup.S19.scene2d; 
import static com.miniproject32.writeup.S19.scene2e; 

//scene editor: generate one for each scene from outline in a sequential order. 
//Store scenes in files. These can be accessed and viewed from home screen 
public class S21 extends AppCompatActivity { 
int sceneno; 
TextView t1,t2; 
EditText e; 

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

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    Bundle extras = getIntent().getExtras(); 
    if (extras != null) { 
     sceneno = extras.getInt("Scene no "); 
     //The key argument here must match that used in the other activity 
    } 
    else 
     sceneno=1; 

    Typeface tf = Typeface.createFromAsset(getAssets(),"font.ttf"); 

    t1 = (TextView) findViewById(R.id.editor_scene_label); 
    t2 = (TextView) findViewById(R.id.editor_scene_val); 

     switch (sceneno) { 
      case 1: 
       t1.setText(R.string.scene_0); 
       t2.setText(scene0); 
       break; 
      case 2: 
       t1.setText(R.string.scene_1a); 
       t2.setText(scene1a); 
       break; 
      case 3: 
       t1.setText(R.string.scene_1b); 
       t2.setText(scene1b); 
       break; 
      case 4: 
       t1.setText(R.string.scene_1c); 
       t2.setText(scene1c); 
       break; 
      case 5: 
       t1.setText(R.string.scene_1d); 
       t2.setText(scene1d); 
       break; 
      case 6: 
       t1.setText(R.string.scene_1e); 
       t2.setText(scene1e); 
       break; 
      case 7: 
       t1.setText(R.string.scene_2a); 
       t2.setText(scene2a); 
       break; 
      case 8: 
       t1.setText(R.string.scene_2b); 
       t2.setText(scene2b); 
       break; 
      case 9: 
       t1.setText(R.string.scene_2c); 
       t2.setText(scene2c); 
       break; 
      case 10: 
       t1.setText(R.string.scene_2d); 
       t2.setText(scene2d); 
       break; 
      case 11: 
       t1.setText(R.string.scene_2e); 
       t2.setText(scene2e); 
       break; 
      case 12: 
       t1.setText(R.string.resolution); 
       t2.setText(resolution); 
       break; 

     } 

    Button b=(Button) findViewById(R.id.but_editor); 
    b.setText(R.string.done); 
    b.setTypeface(tf); 
} 

public void writeStringAsFile(final String fileContents, String fileName) { 
    try { 
     FileWriter out = new FileWriter(new File(this.getFilesDir(), fileName)); 
     out.write(fileContents); 
     out.close(); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

public void nextScreen(View view){ 
    //on click: 
    //save scene 
    //show popup/toast saying scene saved 
    //move to new blank same screen with scene title different 
    e=(EditText) findViewById(R.id.editor_ip); 
     switch (sceneno) { 
      case 1: 
       writeStringAsFile(e.getText().toString(), "Scene1.txt"); 
       Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 
       break; 
      case 2: 
       writeStringAsFile(e.getText().toString(), "Scene2.txt"); 
       Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 
       break; 
      case 3: 
       writeStringAsFile(e.getText().toString(), "Scene3.txt"); 
       Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 
       break; 
      case 4: 
       writeStringAsFile(e.getText().toString(), "Scene4.txt"); 
       Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 
       break; 
      case 5: 
       writeStringAsFile(e.getText().toString(), "Scene5.txt"); 
       Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 
       break; 
      case 6: 
       writeStringAsFile(e.getText().toString(), "Scene6.txt"); 
       Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 
       break; 
      case 7: 
       writeStringAsFile(e.getText().toString(), "Scene7.txt"); 
       Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 
       break; 
      case 8: 
       writeStringAsFile(e.getText().toString(), "Scene8.txt"); 
       Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 
       break; 
      case 9: 
       writeStringAsFile(e.getText().toString(), "Scene9.txt"); 
       Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 
       break; 
      case 10: 
       writeStringAsFile(e.getText().toString(), "Scene10.txt"); 
       Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 
       break; 
      case 11: 
       writeStringAsFile(e.getText().toString(), "Scene11.txt"); 
       Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 
       break; 
      case 12: 
       writeStringAsFile(e.getText().toString(), "Scene12.txt"); 
       Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show(); 
       break; 

      default: 
       break; 
     } 
    sceneno++; 
    Intent intent = new Intent(S21.this,S21.class); 
    intent.putExtra("Scene no ",sceneno); 
    startActivity(intent); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_home_screen, 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_home){ 
     Intent i = new Intent(this, Home.class); 
     startActivity(i); 
    } 
    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

} 

这应该工作的权利?谁能告诉我我错过了什么?

回答

0

每次增加值sceneno ++时。所以它将工作到12案件,但之后,它不显示任何基于代码的概念。因此,在这种开关情况下添加默认像这样

switch (sceneno) { 
     case 1: 

     ..... 
     case 12: 
      t1.setText(R.string.resolution); 
      t2.setText(resolution); 
      break; 
     default : 
      t1.setText("your text"); 
      t2.setText("your text"); 
    } 
+0

等等。这工作!谢谢! – Arshiya

+0

如果它的工作意味着通过点击打勾接受答案,并提高它 – sasikumar