Reputation: 139
with the new ADT from android, the new way of designing layout is using fragment layer.
My problem today however isn't really related to fragment layer. It is in regards to
error opening trace file no such file or directory
Here's my MainActivity code:
package com.skws.calcutil;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
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 display = (TextView) findViewById(R.id.display);
Button btn0 = (Button) findViewById(R.id.btn0);
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
Button btn3 = (Button) findViewById(R.id.btn3);
Button btn4 = (Button) findViewById(R.id.btn4);
Button btn5 = (Button) findViewById(R.id.btn5);
Button btn6 = (Button) findViewById(R.id.btn6);
Button btn7 = (Button) findViewById(R.id.btn7);
Button btn8 = (Button) findViewById(R.id.btn8);
Button btn9 = (Button) findViewById(R.id.btn9);
Button btnDecimal = (Button) findViewById(R.id.btnDecimal);
Button btnAdd = (Button) findViewById(R.id.btnAdd);
Button btnMinus = (Button) findViewById(R.id.btnMinus);
Button btnMultiply = (Button) findViewById(R.id.btnMultiply);
Button btnDivide = (Button) findViewById(R.id.btnDivide);
Button btnEqual = (Button) findViewById(R.id.btnEqual);
btn0.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
display.setText("0");
}
});
}
@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;
}
}
}
The problem is if I removed this section of the code:
btn0.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
display.setText("0");
}
});
This will only load my Layout, which works perfectly. If I add the above code into MainActivity, I'll get that Error.
Is there a new way google wanted us to design with Button Clicks?
I've tried re-installing everything and that didn't work. I've also tried to sync my versions and that didn't work. I've tried to remove the version and add in the SD card permission trick and that didn't work. I've also tried to add an virtual sd space and that didn't work. I've tried my code on my mobile phone and I get the same results.
Any help will be grateful.
Thank you
Upvotes: 1
Views: 2376
Reputation: 139
Ok, I've found the solution.
After comparing with my old codes, it seems that the problem is with the extension of ActionBarActivity.
Once I've moved all the layout into activity_main.xml, and in the Main_Activity.java changing the ActionBarActivity into Activity, it is working again.
Guess that just means that I need to be updated on how to code with Fragments.
Upvotes: 2